Mike-Ward.Net

Console application debugging made easier

When debugging console applications in .NET I like the program to pause just before exit so I can study the console output. One way to do this is to set a break point at the end of the application. This is OK but shifts the focus to the IDE which is annoying, especially in single screen environments like my laptop.

I’ve found a better way. Place a finally block at the end of your program as follows.

class Program
{
    static void Main(string[] args)
    {
        try
        {
            // your code here...
        }

        finally
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                Console.WriteLine("Press [enter] to exit");
                Console.ReadLine();
            }
        }
    }
}

Now the program pauses while debugging allowing you to read the console output.

← newer older →
.Net, Technology, Life, Whatever

Recent Posts

Checklist Buddy Available for Testing
Tweetz 2.0.0 Released
Tweetz 2.0 Beta
VSColorOutput 2.7 - Time Stamps
Fixed Focal-Length Eyeglasses, a Programmer's Best Friend
How to Choose the Right VPN Service
Two Handy Command Line Scripts
More... (1089)

Donate