Coder Perfect

How can I prevent C# console programs from shutting on their own? [duplicate]

Problem

Because my Visual Studio console programs close automatically, I’d like to utilize something like C’s system(“PAUSE”) to “pause” them at the end of their operation. What can I do to make it happen?

Asked by Zignd

Solution #1

Console.ReadLine();

or

Console.ReadKey();

ReadKey() waits for any key, while ReadLine() waits for any line (except for modifier keys).

Darin gave me the key symbol, which I stole.

Answered by Adam

Solution #2

Ctrl+F5 will compile (start debugging) your work for you.

Give it a shot. It’s something I constantly do, and the console always gives me my findings open on it. There is no need for any additional code.

Answered by Silvia Z

Solution #3

Try running your application with Ctrl + F5 in Visual Studio; this will automatically add a pause with “Press any key to continue…” without using the Console. Use the Readline() or ReadKey() functions to read a line or a key.

Answered by Sohail xIN3N

Solution #4

Console. Wait for the user to press Enter or Console before calling ReadLine(). Wait for any key with ReadKey.

Answered by Darin Dimitrov

Solution #5

Use:

Console.ReadKey();

If you want it to close when you touch any key, or if you want it to open when you press any key, or if you want it to

Console.ReadLine();

When the user presses enter after typing anything.

Answered by matthewr

Post is based on https://stackoverflow.com/questions/11512821/how-to-stop-c-sharp-console-applications-from-closing-automatically