Actually I lied in the title, I haven’t learned it today but there was a day when I’ve stumble upon this attribute in someone’s code and started using it ever since. This is one of these things that makes your life much easier while debugging. What does this DebuggerDisplay attribute do exactly?

Let’s start from the beginning. By default when inspecting an object e.g. House:

public class House
{
	public string Street { get; set; }
	public string PostalCode { get; set; }
	public string Number { get; set; }


	public List<Room> Rooms { get; set; }
	public List<Tenant> Tenats { get; set; }	
}

public class Room
{
	public string Name { get; set; }

	public decimal SquereMeters { get; set; }        
	public List<Window> Windows { get; set; }
	public bool HasWindows
	{
		get { return Windows != null && Windows.Any(); }
	}
}

public class Tenant
{
	public string Name { get; set; }
	public string Lastname { get; set; }        
}

public class Window
{
	public string Color { get; set; }
	public decimal Height { get; set; }
	public decimal Width { get; set; }
}

we will se something like this:

default_disply_of_the_object_while_debugging

You can say, but that’s not so bad, because we can inspect this object in details really easily by just expanding information about it, indeed:

checking_the_details_of_the_object

As you know this process, which sometimes cannot be omitted, could drive you mad, especially when you are almost at the end of expanding really complex object and then you miss clicked the last expanding arrow, so everything collapsed, and you’ve to start ‘digging’ from the beginning. Even though it could be a painful process it’s pretty straight forward to inspect whatever information you’re looking for. Things could get bit more annoying when you’re dealing with collections.

default_debugger_display_of_objects

This collection is just an example, of really small one, that still could be handled by this ‘clicking and expanding’ process, but imagine a collection with tens or hundreds of elements (probably you don’t have to imagine anything ‘cause you’re deal with those on regular basis) in which you want to check some deeply hidden property of a child of a child of a child of a child of your object. That would be a nightmare. With help comes DebuggerDisplay attribute which is used as simple as writing something like this

[DebuggerDisplay("Number: {Number}, Street: {Street}, First Room Name: {Rooms[0].Name}")]
public class House
{
	public string Street { get; set; }
	public string PostalCode { get; set; }
	public string Number { get; set; }


	public List<Room> Rooms { get; set; }
	public List<Tenant> Tenats { get; set; }	
}

which gives us an effect of showing whatever information you need (even the one deeply hidden inside an child of a child etc.)

debuggerdisplay_attribute_in_action

Now you’re ready to use it and make your debugging life much easier. BEFORE that, though, you have to be aware of some possibilities and good practices in this subject. You can write basically any code you want in the attribute {string}. This expression is going to be evaluated and than presented while debugging. Previous chunk of code (with DebuggerDisplay attribute) is BADLY written and I’ve put it only for exampling purposes. The first BAD thing about it is that it’s going to evaluate three expressions, and evaluation costs. Second BAD thing is putting a collection indexer reference in it, which can cause exception. So how it should look like ? Like this:

[DebuggerDisplay("{DebuggerDisplay, nq}")]
    public class House
    {
        public string Street { get; set; }
        public string PostalCode { get; set; }
        public string Number { get; set; }


        public List<Room> Rooms { get; set; }
        public List<Tenant> Tenats { get; set; }

        private string DebuggerDisplay
        {
            get
            {
                return string.Format("Number {0}, Street: {1}, First Room Name {2}", Number,
                                                                                     Street,
                                                                                     Rooms != null && Rooms.Any() ? Rooms[0].Name : "No Rooms");
            }
        }
    }

The best practice for DebuggerDisplay attribute is to use as less expression to evaluate as possible and this can be achieved using one property which is going to present your object as you wish. For more specific information(and what this ‘nq‘ thing is doing in the DebuggerDisplay attribute {string}) go to this msdn blog post, which explains in details good and bad practices. It’s not so long and it’s definitely worth reading