Follow us on X (Twitter)  Like us on Facebook  Connect with us on LinkedIn  Subscribe to our YouTube Channel  Subscribe to our WhatsApp Group

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. Thanks to my friend Abhijit Jana for letting me know about this nice trick and thus I thought to share the same with you. I hope that the post was useful and will help you while debugging your code inside Visual Studio IDE.


Connect with me on Twitter, Facebook and Google+ for technical updates. Also subscribe to my blog’s RSS Feed and Email Newsletter to receive updates directly to your inbox.

Have a question? Or, a comment? Let's Discuss it below...


Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.