DebuggerDisplay Attribute

suggest change

Adding the DebuggerDisplay Attribute will change the way the debugger displays the class when it is hovered over.

Expressions that are wrapped in {} will be evaluated by the debugger. This can be a simple property like in the following sample or more complex logic.

[DebuggerDisplay("{StringProperty} - {IntProperty}")]
public class AnObject
{
   public int ObjectId { get; set; }
   public string StringProperty { get; set; }
   public int IntProperty { get; set; }
}

Adding ,nq before the closing bracket removes the quotes when outputting a string.

[DebuggerDisplay("{StringProperty,nq} - {IntProperty}")]

Even though general expressions are allowed in the {} they are not recommended. The DebuggerDisplay attribute will be written into the assembly metadata as a string. Expressions in {} are not checked for validity. So a DebuggerDisplay attribute containing more complex logic than i.e. some simple arithmetic might work fine in C#, but the same expression evaluated in VB.NET will probably not be syntactically valid and produce an error while debugging.

A way to make DebuggerDisplay more language agnostic is to write the expression in a method or property and call it instead.

[DebuggerDisplay("{DebuggerDisplay(),nq}")]
public class AnObject
{
   public int ObjectId { get; set; }
   public string StringProperty { get; set; }
   public int IntProperty { get; set; }

   private string DebuggerDisplay()
    {
        return $"{StringProperty} - {IntProperty}"";
    }
}

One might want DebuggerDisplayto output all or just some of the properties and when debugging and inspecting also the type of the object.

The example below also surrounds the helper method with #if DEBUG as DebuggerDisplay is used in debugging environments.

[DebuggerDisplay("{DebuggerDisplay(),nq}")]
public class AnObject
{
   public int ObjectId { get; set; }
   public string StringProperty { get; set; }
   public int IntProperty { get; set; }

#if DEBUG
   private string DebuggerDisplay()
    {
        return
            $"ObjectId:{this.ObjectId}, StringProperty:{this.StringProperty}, Type:{this.GetType()}";
    }
#endif
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Attributes:
* DebuggerDisplay Attribute

Table Of Contents
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
46 Methods
88 Events
92 Structs
93 Attributes
104 Indexer
106 Stream
107 Timers
109 Threading
127 Caching
135 Pointers
147 C# Script