Previous posts in the series:

In this post I want to show you how to enhance debugging experience. Last time we left with an object which was representing list of method parameters related to the person (for this post I renamed that object to Person). Now, when debugging an application the new object will be shown as a type name, and to see any values you need to expand it:

But there is an easy way to provide custom value which will be displayed in the debugger by default. Lets say for example that the key properties are fullname (the new property we added last time) and age. All you have to do is to add a DebuggerDisplayAttribute to the Person class with a string representing the value to show:

[DebuggerDisplay("{Fullname} ({age})")]
public class Person
{
    // ...
}

As you can see, the string is using property names wrapped in curly braces. Now the debugger will show nicely formatted text:

As you can see, the string is using property names wrapped in curly braces. Now the debugger will show nicely formatted text:

This will work everywhere: when you hover over the variable, in Locals, Autos or Watch debugger windows and Data Tips. Event in the Call Stack window assuming option Show Parameter Values is on (read more about it in this post).

You can show as many properties as you want in the debugger, but there are performance considerations. You can find more details in the Using DebuggerDisplay Attribute article on MSDN. You will also find there a suggested solution which is to create a private method and use it’s result in the DebuggerDisplay:

[DebuggerDisplay("{DebuggerDisplay()")]
public class Person
{
    // ...
    private string DebuggerDisplay()
    {
        return String.Format("{0} (age: {1}, weight: {2})", Fullname, age, weight);
    }
}

See other posts in the series: