Diagnostic Debugger Output

Another capability of the System.Diagnostics namespace is to interact with a debugger from inside your code. Samples include emitting diagnostic information about what your code is doing, which can be visualized in a debugger’s output window. Let’s take a look at some of those capabilities.

Revisiting the Debug class, notice the presence of Write and WriteLine methods. Using them, you can write information to the debugger. Just as with Assert calls, the calls to those methods are omitted from the assembly if it isn’t built using the DEBUG flag, so there’s no runtime overhead in release build binaries. An example is shown here:

Conditional Debugger Output

Variants on Write and WriteLine exist, with an If suffix. Besides ...

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.