Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

console

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 . . .

like image 524
coder Avatar asked Oct 18 '22 01:10

coder


1 Answers

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"

like image 195
Fabjan Avatar answered Oct 21 '22 00:10

Fabjan