1

This may be a stupid question, but why do I need to use Console.ReadLine() to prevent the console from closing immediately? for instance, this program:

    namespace hello_world
    {
        class Program
        {
    static void Main(string[] args)
            {
                Console.WriteLine("Hello World!");
                Console.ReadLine();//without this, the console closes immediately
            }
        }
    }

The console closes almost as soon as it opens. My book doesn't compensate with this, and the Udemy video I am watching doesn't compensate with it either. None of my books C# even addressed this. I am using visual studio community 2015.

Tommy
  • 59
  • 2

2 Answers2

1

The console closes almost as soon as it opens.

Normally, this does not happen. This only happens when you run your app by pressing the start button with the little green triangle. Because what that button does is "Start with debugging". If you start with debugging, the console window closes after the program has finished running. To start without debugging, go to Debug -> Start without debugging

Sweeper
  • 145,870
  • 17
  • 129
  • 225
0

That's because without that line your program has completed. It prints your "Hello World!" and instantly returns. When you add that line you're waiting for the input to read.

AMZ
  • 520
  • 4
  • 15