Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Exceptions and Diagnostics

Debugger attributes

Reference for C# debugger attributes: DebuggerDisplay, DebuggerBrowsable, DebuggerTypeProxy, DebuggerStepThrough, DebuggerHidden and the Debugger class.

The attributes in System.Diagnostics let you control how a debugger (Visual Studio, Rider, VS Code) shows your types and steps through your code. They do not change what your program does when it runs normally; they are hints stored in the assembly metadata.

The most useful one is [DebuggerDisplay], which sets the one-line text shown next to an object in watch windows and tooltips. [DebuggerBrowsable] hides fields, [DebuggerTypeProxy] replaces the expanded view with a friendlier class, and [DebuggerStepThrough] / [DebuggerHidden] keep boring helper code out of your step-by-step sessions.

C#
[DebuggerDisplay("{Name,nq} ({Age})")]
class Person { public string Name = ""; public int Age; }

Try it

C# Reading the attributes at run time

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# DebuggerTypeProxy and a safe Debugger.Break

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
[DebuggerDisplay("text with {Expression} and {Other,nq}")]
[DebuggerBrowsable(DebuggerBrowsableState.Never | Collapsed | RootHidden)]
[DebuggerTypeProxy(typeof(ProxyClass))]
[DebuggerStepThrough]   [DebuggerHidden]   [DebuggerNonUserCode]
Attribute Effect
DebuggerDisplay The text shown for the object. Anything in {} is evaluated as an expression; ,nq removes the quotes around a string. Applies to classes, structs, fields, properties, enums and assemblies.
DebuggerBrowsable Controls whether a field or property appears when you expand the object: Never, Collapsed, RootHidden (show the children directly).
DebuggerTypeProxy Shows an instance of the proxy class when the object is expanded. The proxy takes the real object in its constructor.
DebuggerStepThrough Step-into skips the method body; breakpoints inside it still work.
DebuggerHidden Like step-through, but breakpoints inside are ignored too.
DebuggerNonUserCode Marks code as library code, so "Just My Code" skips it.
Debugger.IsAttached true when a debugger is attached to the process.
Debugger.Break() Stops in the debugger like a breakpoint; does nothing when no debugger is attached.
Debugger.Launch() Asks the operating system to start a debugger and attach it. Avoid in production code.

Returns: Debugger.IsAttached returns a bool; the attributes themselves do not return anything.

Errors: an invalid expression inside a DebuggerDisplay string is not a compile error. The debugger shows an error text instead, so test the display in a real session.

Usage notes

  • Display expressions are evaluated by the debugger, not by your program, and they can have side effects or cost time. Keep them to simple properties, or point to a private DebuggerDisplay property that builds the text.
  • Records and anonymous types already get a useful display (Person { Name = Ada, Age = 36 }), so add [DebuggerDisplay] mostly to ordinary classes and collection wrappers.
  • Do not put side effects in the proxy. Reading Items must not change the object, because the debugger reads it whenever you expand the node.
  • DebuggerStepThrough suits trivial plumbing such as forwarding methods and generated code; do not put it on code you may need to debug.
  • [Conditional("DEBUG")] on a method is a different tool: the call is removed from Release builds (see built-in attributes).
  • Debugger.Break() and Debugger.Launch() in shipped code are risky; guard with IsAttached and prefer a real breakpoint.
  • Cost: attributes add a small amount of metadata and no run-time work in normal execution.