Why does my console application disappear immediately

I created the Microsoft Visual Studio 2012 console application. This is what I added to my code.

    static void Main(string[] args)
    {
        Console.WriteLine("Hello World");
    }

When I debug this program, the program immediately closes! What am I doing wrong?

+4
source share
7 answers

You must add Console.ReadLine()at the end of the method Main, this will make the console wait until the user sends any input to the console, and then it exits.

+3
source

Al existing answers are correct, but Ctrl-F5 (and not just F5 in VS) will also have the effect of waiting for a keystroke to start.

EDIT

, . F10 .

+2

Console.WriteLine("Hello World");
Console.ReadLine();
0

, , , . Console.ReadLine , , . Enter.

ReadKey, : " , ..." , , .

static void Main(string[] args)
{
    Console.WriteLine("Hello World");
    Console.Read();
}
0

Console.ReadLine(); WriteLine, . .

   static void Main(string[] args)
    {
        Console.WriteLine("Hello World");
        Console.ReadLine();
    }
0

You can just add

Console.ReadKey();

This will wait for the key to be pressed before closing the program.

0
source

all of the above is correct. Typically, Console.ReadKey () is used for control instead of logical processing.

static void Main(string[] args)
{
    Console.WriteLine("Hello World");
    string line = Console.ReadKey();
}

you can get more information here

0
source

Source: https://habr.com/ru/post/1542290/


All Articles