The DebuggerDisplayAttribute Class controls how an object, property, or field is displayed in the debugger variable windows. This attribute can be applied to types, delegates, properties, fields, and assemblies.
The DebuggerDisplay attribute has a single argument, which is a string to be displayed in the value column for instances of the type. This string can contain braces ({ and }). Text within a pair of braces is evaluated as a field, property or method.
Example
So here I’m going to have a Person class. I’m going to define the attribute to show the firstname and lastname.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Now when we debug, this is the result when we look at the new person instance.
Best Practice?
In most open source projects, it appears one of the practices many employ is creating an internal DebuggerDisplay that returns the relevant string you want to display.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Notice the “nq” in the DebuggerDisplay Attribute, this is to simply say “No Quotes”. Here is the resulting output:
More
There are so many things like this attribute that either we just don’t know about or forget about along the way. If you have any other suggestions please leave a comment here or on twitter.