Greg Beech's Website

Tips & Tricks: Use DebuggerDisplayAttribute for easier debugging

When debugging, it's common to hover over an object to get a quick view of what its properties are. With a single object this is fine because you can easily expand it to view its contents, but when you have a collection of objects it's painful to see the contents as you have to expand every element. The typical view in the debugger of a collection of simple movie classes is like this:

public class Movie
{
    public int Id { get; set; }
    public string Title { get; set; }
    public float Rating { get; set; }
}

 Debugger default view

This is where DebuggerDisplayAttribute comes in; by applying it to our Movie class the collection hover can be made much more informative. The format string looks similar to normal .NET format strings in that the values to replace are between curly braces, but instead of being told what to substitute the debugger treats these values as expressions and attempts to evaluate them using the current context. Here we simply list the properties of the object:

[DebuggerDisplay("Id = {Id}, Title = {Title}, Rating = {Rating}")]
public class Movie
{
    public int Id { get; set; }
    public string Title { get; set; }
    public float Rating { get; set; }
}

Debugger display with attribute expressions  

Where this gets really interesting, however, is that with the C# debugger you can enter much more powerful expressions to evaluate than just the property names. If we didn't want to have really long titles listed in full (because "The assassination of Jesse James by the coward Robert Ford" might scroll off all but the widest of monitors) we could truncate them to 20 characters and show an ellipsis to indicate the truncation:

[DebuggerDisplay(
    "Id = {Id}, " +
    "Title = {Title.Substring(0, System.Math.Min(Title.Length, 20)) + (Title.Length > 20 ? \"\" : \"\")}, " +
    "Rating = {Rating}")]
public class Movie
{
    public int Id { get; set; }
    public string Title { get; set; }
    public float Rating { get; set; }
}

 Debugger display with complex expressions

There is a down-side to this power though - because evaluation of the DebuggerDisplayAttribute format string is compiler-specific, the first example will work fine most other debuggers such as VB, but the second one will not evaluate due to its use of the C#-specific ternary operator. Depending on the debugger this will cause it to display either the default value of the class name, or an error indicating which part of the syntax could not be evaluated; this may be important if you work in a multi-language environment, or if you ship your classes as a library to customers.


Posted Jul 28 2008, 09:44 PM by Greg Beech
Filed under: ,

Comments

Sean Kearon wrote re: Tips & Tricks: Use DebuggerDisplayAttribute for easier debugging
on 07-29-2008 5:47 PM

Sweet, very simple, very useful!  I'd always used overridden ToString() to acheive this, ignoring the part of me that questioned whether ToString() was not far more important than to be dedicated to the debugger!  

Thanks!

Omar Kooheji wrote re: Tips & Tricks: Use DebuggerDisplayAttribute for easier debugging
on 09-12-2008 11:58 AM

Thanks for the info, I'll update the post on my blog (or write another post)  to show this more correct way to do this.

DotNetKicks.com wrote Tips
on 09-12-2008 8:38 PM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DebuggerDisplayAttribute Makes Debugging Clearer « dotNetChris wrote DebuggerDisplayAttribute Makes Debugging Clearer « dotNetChris
on 09-12-2008 9:27 PM

Pingback from  DebuggerDisplayAttribute Makes Debugging Clearer « dotNetChris

alvego wrote re: Tips & Tricks: Use DebuggerDisplayAttribute for easier debugging
on 09-15-2008 10:47 AM

Thanks big. I was very much helped by your information.

Code Monkey Labs wrote Weekly Web Nuggets #30
on 09-22-2008 5:22 AM

General Some IoC Container Guidelines : Jimmy Bogard presents some good guidelines for working with your IoC container of choice. Lambdas – Know Your Closures : Jason Olson exposes a tricky behavior of lambdas that can bite even folks that consider themselves

Code Monkey Labs wrote Weekly Web Nuggets #30
on 02-23-2009 3:46 AM

General Some IoC Container Guidelines : Jimmy Bogard presents some good guidelines for working with your IoC container of choice. Lambdas – Know Your Closures : Jason Olson exposes a tricky behavior of lambdas that can bite even folks that consider

Tramadol side effects. wrote Tramadol.
on 03-27-2009 6:50 AM

Tramadol drug. Buy tramadol. White tramadol with 377 on the side. Tramadol hcl. Tramadol. Tramadol 50mg.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:
Copyright (C) Greg Beech. All rights reserved.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems