Speed up debugging using the DebuggerDisplayAttribute

Today’s post is written by guest blogger Albin Sunnanbo. He’s a great friend and source of inspiration and also one of the best developers I know. It was Albin that first introduced me to LINQ, EF Migrations and jQuery that I now use daily. In this post Albin shows how to use the [DebuggerDisplay] attributes to make debugging easier.

When debugging an application with lots of data, you often find yourself digging through long lists of data to find a specific item or verifying a specific property of one or more items. When using Data Tips, Watch or Quick Watch in Visual Studio you often find a pretty anonymous list of objects where you need to expand each item in the list until you find an object with the specific properties you need for your debugging scenario.

Here I have a LINQ2SQL context for my AdventureWorks demo database. I have a simple LINQ query for some products in the database and I want to find the product “Blade” that I previously created to verify that all properties was saved correctly to the database.

I brought up the Data Tip for my products list and start expanding/collapsing items. Quite boring work if you ask me.

Fortunately there is a better way: the DebuggerDisplayAttribute.

The DebuggerDisplayAttribute attribute provides a special formatting string that tells Visual Studio how to present the content of a class in the debug windows. The formatting string contains plain text and placeholders in curly braces {name}. The name in the curly braces will be mapped to properties, fields or methods matching that name.

Since my LINQ2SQL context may be regenerated I don’t want to put my attributes in the generated files, instead I create a separate code file with an empty partial class definition for the class I want to provide an attribute for.

using System.Diagnostics;
namespace AdventureWorks
    [DebuggerDisplay("Name: {Name}, ProductNumber: {ProductNumber}")]
    public partial class Product

Now when I open up my Data Tip each Product is formatted with the information I need to quickly find the item I care about.


When you provide DebuggerDisplayAttributes for your own classes you don’t need to write the attributes in partial classes, just put them on top of your main class definition.

An alternative to the DebuggerDisplayAttribute is to override the ToString method in your classes. If you don’t have an DebuggerDisplayAttribute the debugger uses the ToString method, but I think the usage of the attribute is more clear in intent and saves ToString for other purposes.

Typical use

Most of my classes does not have the DebuggerDisplayAttribute, not even all entity classes. I only add the attribute when I need it. The main reason to add the attribute is that I have long lists of objects and I need to inspect the content of some specific objects or a specific property on all objects.

Sometimes I also add the attribute to classes where I only have a single instance at a time but want to pack a lot of information in a single row in the watch window.

Typically I print out the 2-5 most important properties of an object in the format string.

  • Leave a Reply

    Your name as it will be displayed on the posted comment.
    Your e-mail address will not be published. It is only used if I want to get in touch during comment moderation.
    Your name will be a link to this address.
Software Development is a Job – Coding is a Passion

I'm Anders Abel, an independent systems architect and developer in Stockholm, Sweden.

profile for Anders Abel at Stack Overflow, Q&A for professional and enthusiast programmers

Code for most posts is available on my GitHub account.

Popular Posts



Powered by WordPress with the Passion for Coding theme.