Debug your collections with DebuggerDisplay...

Let's have a look at DebuggerDisplay, a super nice debugging feature which is not known enough...

/Debugging/2005.jpg

Debugging Collections

Let's say you are in the middle of a method, which has a collection of Person instances. You just got this collection from somewhere else, and you want to see what you received.

The usual experience is like: you hover your mouse over the collection and you click the expand button. And you see that you have a collection of Person instances. Something you already knew. If you want to look inside the instances you now have to click the expand button for each instance... This sucks!

/Debugging/Debugging1.png

Introducing DebuggerDisplay

But you can have another experience. You add the DebuggerDisplay debugging attribute to your class. With this attribute you can configure how the debugger will display each instance, and you can include properties simply by wrapping them in curly braces!

[DebuggerDisplay("Person {Name} - {Age}")]
class Person
{
  public string Name { get; set; }

  public int Age { get; set; }
}

Now you hover (while debugging) over the collection, and you click the expand button. Now you can look at each instance and see what you need!

/Debugging/Debugging2.png

Hey! Thank you! This is a nifty new feature in Visual Studio?

No, this was released in 2005.

If you want to learn more, here is the new documentation.