Fixing debugging displays with ToString()
Debugging can be tricky when you’re trying to inspect a collection of objects that superficially all look the same! But there’s a few things you can do to alleviate that problem, and a few gotchas to try and avoid, too.
Lets say you have a simple class called TimeRange, which defines a start time and an end time, and also has a calculated property for the length of time it represents. Your class might look something like this:
public class TimeRange
{
public TimeSpan Start { get; set; }
public TimeSpan End { get; set; }
public TimeSpan Length
{
get { return End.Subtract(Start); }
}
}
Now, lets say you create a list of these Time Ranges. When you debug your file, inspecting the list will give you a picture like this:
That’s not very helpful now, is it?! It would be much easier to work out what was going on if the debugger showed us a little bit about each time range, rather than simply the object type’s full name.
Thankfully, it’s actually pretty easy to customise what the debugger displays during object inspection using one of two methods:
- The “hard” way: Decorate your class with the
DebuggerDisplayAttribute(which you can read about here). Essentially, this allows you to write custom text, and/or an expression which will be evaluated by the debugger when inspecting an object. - The “easy” way: Override the
ToString()method! This is what the debugger uses by default to display information about your object.
(Note: If you override the ToString() method as well as decorate your class with the DebuggerDisplayAttribute, the attribute will take precedence.)
So, with that in mind, lets override ToString() to fix our debugging display:
override public string ToString()
{
return String.Format("{0:hh:mm} - {1:hh:mm} (Length: {2:h}h {2:mm}m)",
Start, End, Length);
}
And now lets run our debugger and check out… hey, wait a minute! Our debugging display hasn’t changed! What’s going on? I thought overriding ToString() would change what the debugger shows when inspecting our objects! Lets use a hammer to crack a nut, and add in the DebuggerDisplayAttribute as well:
[DebuggerDisplay("{ToString()}")]
public class TimeRange
And now lets see what we get:
Ah ha! We have a problem with our String.Format() statement – it turns out the formatting strings that we’ve used for the TimeSpan instances are incorrect. So, lets fix our formatting string, and remove the DebuggerDisplay attribute. Here’s how our class looks now:
public class TimeRange
{
public TimeSpan Start { get; set; }
public TimeSpan End { get; set; }
public TimeSpan Length
{
get { return End.Subtract(Start); }
}
override public string ToString()
{
return String.Format(@"{0:hh\:mm} - {1:hh\:mm} (Length: {2:%h}h {2:mm}m)",
Start, End, Length);
}
}
And here’s what we see when we inspect a list of Time Ranges in the debugger:
Much better!



Here is some info on debuggerdisplayattribute best practices as well -> https://blogs.msdn.com/b/jaredpar/archive/2011/03/18/debuggerdisplay-attribute-best-practices.aspx