Click here to Skip to main content
15,885,875 members
Articles / .NET
Tip/Trick

Enhancing Debugging with the Debugger Display Attributes

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
6 Nov 2011CPOL1 min read 11.5K   3   1
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
C#
[DebuggerDisplay("{ID}=>{Name}")]

Used on a type with fields ID and Name. 7=>Pronojit
C#
[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:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Leadscale Engine Limited
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 5 Thanks Pin
rm8227-Nov-11 10:11
rm8227-Nov-11 10:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.