I often see questions asking how to pause a console program being debugged.
Visual Studio (and its predecessor) will pause a console program being executed
from within it without the debugger. That is different from the behavior that we see
when the program is executed somewhere else, such as from Windows Explorer and
from "Run" in the Start Menu. Visual Studio does automatically pause a console program when
the program is being debugged. So we usually need to do something separate to
pause a console program being debugged so we can see the results.
What I usually do is to put a breakpoint on the last line of Main, or
somewhere like that.
Often the suggestion is to use system("pause") or Console.ReadKey(). Since
that will always cause the program to pause even if the
program is not being debugged, it can be slightly irritating. We certainly
do not want to leave that in a production program. A good solution is to use Debugger.IsAttached
in combination with Console.ReadKey() to determine if
the console program is being
debugged. The following is a simple sample of how it can be used.
static void Main(string args)
Console.WriteLine("Debugging; press a key to continue...");