Lately I've been exploring the System.Diagnostics namespace (previously I've posted about how to get the current stack trace), and one class that I've discovered which is pretty cool is the Debugger class. Essentially, it allows you to communicate with any debugger that is currently attached to the process. Apart form letting you send log messages to the debugger, it allows you to signal a breakpoint aswell - basically you can say:
Debugger.Break();
anywhere in your code, and the debbuger will act as if there was a breakpoint on that line of code, making the application break into it. That's pretty cool - but what's even cooler, is that if no debugger is attached, you will get the following prompt:
Hit debug, and you will be able to attach a debugger and then break into the code. Now you might not want to do this in production code - so you should probably wrap the Debugger.Break() call in a method decorated with the Conditional attribute and the condition "DEBUG" (which I consider a better practice than #if DEBUG), which will ensure that it wont get called if you're running a release build:
[Conditional("DEBUG")]
public static void BreakIntoDebugger()
{
Debugger.Break();
}