Debug Class vs Debugger Class in C#

The Debug class and Debugger class in C# are both part of the System.Diagnostics namespace but serve different purposes. The Debug class provides methods for conditional debugging output, while the Debugger class enables communication and interaction with debuggers attached to your application.

Debug Class

The Debug class is a static class that provides conditional compilation methods for debugging. It outputs information only when the DEBUG symbol is defined during compilation −

public static class Debug

Properties of Debug Class

Property Description
AutoFlush Gets or sets a value indicating whether Flush should be called on the Listeners after every write.
IndentLevel Gets or sets the indent level for debug output formatting.
IndentSize Gets or sets the number of spaces in an indent.
Listeners Gets the collection of listeners that monitor the debug output.

Example

using System;
using System.Diagnostics;

class Program {
   public static void Main() {
      Debug.WriteLine("Debug output - this appears in debug mode");
      Debug.WriteLineIf(true, "Conditional debug output");
      
      Debug.IndentLevel = 1;
      Debug.WriteLine("Indented debug message");
      
      Debug.Assert(5 > 3, "This assertion passes");
      Debug.Assert(2 > 5, "This assertion would fail in debug mode");
      
      Console.WriteLine("Program execution completed");
   }
}

The output of the above code (in debug mode) is −

Program execution completed

Debugger Class

The Debugger class is a sealed class that provides communication with debuggers. It cannot be inherited and offers methods to interact with attached debuggers −

public sealed class Debugger

Properties of Debugger Class

Property Description
IsAttached Gets a value indicating whether a debugger is attached to the current process.

Example

using System;
using System.Diagnostics;

class Program {
   public static void Main() {
      if (Debugger.IsAttached) {
         Console.WriteLine("Debugger is attached");
         Debugger.Break(); // Triggers a breakpoint
      } else {
         Console.WriteLine("No debugger attached");
      }
      
      Console.WriteLine("Checking if debugger is present: " + Debugger.IsAttached);
      Console.WriteLine("Program continues execution");
   }
}

The output of the above code is −

No debugger attached
Checking if debugger is present: False
Program continues execution

Debug Class vs Debugger Class Debug Class ? Static class ? Conditional compilation ? WriteLine(), Assert() ? Output formatting ? Debug mode only ? AutoFlush, Listeners Debugger Class ? Sealed class ? Debugger communication ? Break(), Launch() ? Runtime debugging ? IsAttached property ? Process interaction

Key Differences

Debug Class Debugger Class
Static class for conditional debugging output Sealed class for debugger communication
Works only when DEBUG symbol is defined Works at runtime regardless of compilation mode
Methods: WriteLine(), Assert(), WriteIf() Methods: Break(), Launch(), IsAttached
Used for logging and output formatting Used for debugger interaction and control

Conclusion

The Debug class is used for conditional debugging output and works only in debug builds, while the Debugger class provides runtime interaction with attached debuggers. Use Debug class for logging debug information and Debugger class when you need to programmatically control debugger behavior.

Updated on: 2026-03-17T07:04:35+05:30

445 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements