Debugger.Launch (.Net)

Now and then I come across a feature in .Net that I have managed to completely miss over the years. Whilst trying to figure out why a unit test was failing when running it, but passing whilst debugging, I came across this nifty feature.

Debugger.Launch will attempt to break the program execution and give you the chance to attach a debugger so you can examine what’s going on.

if (condition)
{
    Debugger.Launch();
}

If the code is already running with a debugger attached, nothing will happen when the code is hit. If you want the code to break whether or not you have a debugger attached, you can use Debugger.Break instead.