coder
coder

Reputation: 51

Explanation of 'Press Any key to continue.... '

It just came to attention that every time we run the c# console application, at the end it shows text stating "Press any key to continue... ".

And the moment you hit any key, it terminates the console/program. In actual program there is no mentioning about such text printing on standard output console, then from where and why it comes out on screen?

Can someone explain the logic behind?

Code:

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

Output:

Test Application

Press any key to continue . . .

Upvotes: 5

Views: 6602

Answers (3)

Fabjan
Fabjan

Reputation: 13676

It has nothing to do with your application itself. When you double-click on the output EXE file you'll not see it. It is only when we run the app from within Visual Studio without the debugger attached when this behavior is seen.

When you press Ctrl+F5, Visual Studio is running your app in a way that causes the console window to remain open.

I think it comes from cmd parameters that are used. Something like :

%COMSPEC% /k "C:\VS\MyApplication.exe"

Upvotes: 3

Pav
Pav

Reputation: 276

In normal application the console will close when done and that is the expected behaviour. This prompt simply helps you to check the results of your code when you are writing your application and you do not have to put (and remember about later removal) of the:

Console.ReadLine();

in your application just to test it and see what the output is.

Upvotes: 0

RisuRyu
RisuRyu

Reputation: 134

Do you use CodeBlocks ?

If yes, it is a Feature of CodeBlocks. That you can read the Output without to write something like getChar() at the end that the console stay open. Otherwise it would close instantly and you can't read the output.

Upvotes: 0

Related Questions