Click here to Skip to main content
13,287,856 members (42,421 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


8 bookmarked
Posted 12 Oct 2010

Using DebuggerDisplayAttribute

, 12 Oct 2010
Rate this:
Please Sign up or sign in to vote.
Using DebuggerDisplayAttribute

In .NET, we can view variables' contents in different ways like local window, quick watch, etc. In System.Diagnostics namespace, we can see a DebuggerDisplayAttribute class that is used to view variables’ contents in debugger window. This attribute can be applied to class, field, property, enum, delegate, struct, etc. Using this attribute, we can easily view variable contents in debug mode, and those contents are easily visible as data tip when we move mouse pointer over that variable. This attribute becomes quite useful when we have to view inner contents of custom type object variable when it has a collection of values.

For example, we will apply this attribute to a class and watch the collection values in debug window.

[DebuggerDisplay("Client Name = {CustomerName} Client Type = {CustomerType, nq}")]
class Customer
private string _CustomerName;
private string _CustomerType;

public Customer(string strCustomerName, string strCustomerType)
_CustomerName = strCustomerName;
_CustomerType = strCustomerType;

public string CustomerName
get { return _CustomerName; }

public string CustomerType
get { return _CustomerType; }

Now after loading a Customer type collection, we see the following view in data tip:


Fig 1: DebuggerDisplayAttribute changing the data view.

Had we not used DebuggerDisplayAttribute on Customer class type, we would have to traverse a long hierarchy of tree view of each index value of collection objects to view data contents.

Programmers often override ToString() method in the custom class type method to view data. But still DebuggerDisplayAttribute wins the heart!

When ToString() method is overridden inside Customer class, then ToString() method of Customer object will result as:

public new string ToString()
return ("Customer Name: " + _CustomerName + "\n" + "Customer Type: " + _CustomerType);

ToString Method

Fig 2: Result of overridden ToString() method.

DebuggerDisplayAttribute constructor has only one parameter as string. The {} braces contain field or property or method name. In the example above, we have used this way.

[DebuggerDisplay("Client Name = {CustomerName} Client Type = {CustomerType, nq}")]

One can also quickly see {CustomerType, nq}. Due to this “nq” specifier, Client Type value is shown without double quotes, whereas Client Name value is still in double quote (see Fig 1). The “nq” specifier is used for string type properties.

Happy debugging for next time!

Posted in .NET Technologies, C#/VB.NET, CodeProject, Dot NET Tips Tagged: .NET 3.5, C#, Debugging


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


About the Author

Dinesh K Mandal
Technical Lead Imfinity India Pte Ltd, Noida (Excelsoft Company)
India India

You may also be interested in...


Comments and Discussions

GeneralSo how to Trace? Pin
A Berglas14-Jun-11 17:27
memberA Berglas14-Jun-11 17:27 
GeneralMy vote of 5 Pin
vladisvit21-Oct-10 22:34
membervladisvit21-Oct-10 22:34 
GeneralMy vote of 5 Pin
Kim Togo19-Oct-10 5:20
memberKim Togo19-Oct-10 5:20 
GeneralVery Useful Pin
HuntrCkr18-Oct-10 19:48
memberHuntrCkr18-Oct-10 19:48 

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.171207.1 | Last Updated 12 Oct 2010
Article Copyright 2010 by Dinesh K Mandal
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid