Improving debugging experience in Visual Studio by using DebuggerDisplay attribute

Debugging is one of the most important part of developing software. Having good tooling for debugging can turn a bug hunt from hours to just a few minutes. Visual Studio debugger is already great out of the box, but it provides extensibility points which allow to even improve the debugger.

In this post I will show how you can use the DebuggerDisplay to get quicker important information when debugging in Visual Studio.

The out of the box experience

When debugging code in Visual Studio, you get information about instances of object in a small preview window.

image-20200808213617330

Since it only shows the name of the class and its namespace it is not helpful. If you need more information you must collapse the item. And if you just want to check if a collection has a specific item you must do a lot of clicks.

… and with DebuggerDisplay

Luckily, Visual Studio support the “DebuggerDisplay” attribute. With just a simple line you can print more useful information inside the preview window.

image-20200808210515692

The attribute is pretty simple:

[DebuggerDisplay("Date = {Date} Temperatur = {TemperatureC}")]
public class WeatherForecast
{
    public DateTime Date { get; set; }

    public int TemperatureC { get; set; }

    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

    public string Summary { get; set; }
}

The attribute takes just one string, which can contain C# string formats. For more information checkout the official documentation .