Simple Debugging

In its simplest form, runtime debugging is just a matter of printing out statements to the console. The Debug class, a member of the System.Diagnostics namespace, has two methods for supporting explicit debugging: Write() and WriteLine(). These methods work similar to their Console class counterparts. Listing 31.1 shows an example that uses the WriteLine() method of the Debug class.

Listing 31.1. A Simple Debugging Example: PlainDebugDemo.cs
 #define DEBUG using System; using System.Diagnostics; /// <summary> /// Plain Debug Demo. /// </summary> class PlainDebugDemo { static void DebuggedMethod() { Debug.WriteLine("Debug: Entered MyMethod()"); } static void Main(string[] args) { TextWriterTraceListener myListener = new TextWriterTraceListener(Console.Out); ...

Get C# 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.