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

Mithril Rising
Tweetz Release 1.2.4 - Screen Off Detection
Tweetz Desktop Support for 280 Characters
Quick Tip - Clear ReSharper's Cache to Fix False Errors
Quick Tip - Refined GitHub
Vertical Layout for HTML Tables
Visual Studio 2017 Installer Quick Tip
More... (1080)

Donate