Over a million developers have joined DZone.

Debugging Code Efficiently in Visual Studio with the DebuggerDisplay Attribute

DZone's Guide to

Debugging Code Efficiently in Visual Studio with the DebuggerDisplay Attribute

· Performance Zone ·
Free Resource

Sensu is an open source monitoring event pipeline. Try it today.

It is sometime difficult when you have a bunch of properties exposed from a class and you just need few of them while debugging a code block. Sometimes it becomes even more complex when you have a collection of that class.

Is there any easiest way to improve the code debugging in Visual Studio? Yes, there is. In this post we are going to learn this technique if you have never used it yet.

Code debugging is not a child’s play and it’s becomes difficult when debugging a code written by someone else. In such case, if your boss assigns a bug to you and asks to fix as priority basis, it may come as thunderstorm. While debugging the code, you notice that it’s an issue in a collection where the values are not properly assigned, you have to break your head pinning the items in the Visual Studio debugger and checking individual properties/variables in multiple levels. Just think about a scenario like this:

Debugging individual item in a Collection

In the above scenario, you have to go thru individual items to check whether the collection has proper data assigned to it. Isn’t it a difficult job? Yes it is. So, what’s the approach? Can’t we simplify this in Visual Studio? Have you ever think these when you debugged through your code?

Ok, let’s see how we can simplify this. To begin with, we will first go and create a simplest class called “Person” with three properties in it named “Firstname”, “Lastname” and “Age” as mentioned in the following code snippet:

Person class

Once your class is ready, create an instance of the class and populate the values in it. Now let’s debug through the code and see how we generally debug the Person object here:

Debugging Person Object in Multiple Levels

As shown above, we are expanding the object to check the property values. As it’s a simple object, we might find it good but think about a scenario having multiple properties and variables inside it. Debugging that becomes very difficult that you already know.

To solve this problem, there is an attribute named “DebuggerDisplay” present in the “System.Diagnostics” namespace which determines how a class of field displays in the debugger window. You need to add this attribute to the class level with properly formatted with the values that you want to display.

Here is an example of what we want to display for the class:

Setting a DebuggerDisplay Attribute to the Person Class

The value inside the { } defines the property that you want to show in the debugger window. Thus running the code now will display the Firstname, Lastname and Age properties formatted properly in the class level and you don’t have to dig through the object to find out the actual value.

Here is a snapshot of what you will see after running this code with DebuggerDisplay attribute set:

Debugging Person Object with Display Values in First Level

As I mentioned earlier, it is quite helpful when debugging a collection of objects where you want to check few properties without digging each individual objects. Debugging a collection of objects now inside the debugger window will display them well structured in proper format as shown below:

Debugging a Collection of Objects using the DebuggerDisplay Attribute

This way, you don’t have to check out each and individual object by expanding it. You will see them all aligned properly. This now made my life easy in the breakpoints. I hope that the post was useful and will help you while debugging your code inside Visual Studio IDE.

Sensu: workflow automation for monitoring. Learn more—download the whitepaper.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}