65.9K
CodeProject is changing. Read more.
Home

Enhancing Debugging with the Debugger Display Attributes

starIconstarIconstarIconstarIconstarIcon

5.00/5 (4 votes)

Nov 6, 2011

CPOL

1 min read

viewsIcon

11980

Enhancing Debugging with the Debugger Display Attributes

Yesterday, I found something which is very interesting for me. After working on .NET for the last 9 years, I just found DebuggerDisplayAttribute which may help many developers while debugging the application. This post has been created by copying lots of text from online MSDN, but may help some developers without reading the whole post.

DebuggerDisplayAttribute

The DebuggerDisplayAttribute constructor has a single argument: a string to be displayed in the value column for instances of the type. This string can contain braces ({ and }). The text within a pair of braces is evaluated as the name of a field, property, or method. This attribute can be applied to: Classes, Structs, Delegates, Enums, Fields, Properties, Assemblies. In C# code, you can use a general expression between the braces. The expression has implicit access to the this pointer for the current instance of the target type only. The expression has no access to aliases, locals, or pointers. If the expression references properties, attributes on those properties are not processed. The following shows some possible uses of the DebuggerDisplay attribute and example output. Attribute Output appearing in the Value column
[DebuggerDisplay("{ID}=>{Name}")]
Used on a type with fields ID and Name. 7=>Pronojit
[DebuggerDisplay("Object {count - 2}: {(flag) ? \"yes\" : \"no\"}")]
Expression syntax varies between languages. Therefore, use it with care. Object 6: yes If a C# object has an overridden ToString(), the debugger will call the override and show its result instead of the standard {}. Thus, if you have overridden ToString(), you do not have to use DebuggerDisplay. If you use both, the DebuggerDisplay attribute takes precedence over the ToString() override. Whether the debugger evaluates this implicit ToString() call depends on a user setting in the Options dialog box (Debugging category, General page). FOR MORE: