Performance Zone is brought to you in partnership with:

Kunal Chowdhury is a Microsoft MVP (Most Valuable Professional) in Windows Platform Development, Telerik MVP and Insiders, a Codeproject Mentor, DZone's MVB (Most Valuable Blogger), Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession. He shares his technical findings in his Blog: He is also available in Twitter: Kunal is a DZone MVB and is not an employee of DZone and has posted 16 posts at DZone. You can read more from them at their website. View Full User Profile

Debugging Code Efficiently in Visual Studio with the DebuggerDisplay Attribute

  • submit to reddit

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.

Published at DZone with permission of Kunal Chowdhury, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Arthur Gorr replied on Wed, 2013/02/13 - 11:00am

I have been debugging code in Visual Studio for many years and recognize the pain here.  I never knew about DebuggerDisplay.  This looks awesome and I will add to my tools quiver.  Thank you. I love learning productivity tips!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.