Click here to Skip to main content
13,089,504 members (61,507 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 6 Nov 2011

Enhancing Debugging with the Debugger Display Attributes

, 6 Nov 2011
Rate this:
Please Sign up or sign in to vote.
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.


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

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).



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


About the Author

Pronojit Roy
Chief Technology Officer GoAllOver Limited
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralReason for my vote of 5 Thanks Pin
rm8227-Nov-11 10:11
memberrm8227-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170813.1 | Last Updated 6 Nov 2011
Article Copyright 2011 by Pronojit Roy
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid