DebuggerDisplay Attribute

Team System June 3rd, 2009

The DebuggerDisplay attribute (System.Diagnostics.DebuggerDisplayAttribute) controls how a class or field is displayed in the debugger variable windows. When a custom object is displayed, the name of the class or the result of ToString() is shown. This can be changed with a DebuggerDisplay attribute.

Look at the following demonstration. I added the DebuggerDisplay attribute:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace DebuggerView
{
    [DebuggerDisplay("User name={UserName} " +
        "& email={EmailAddress}")]
    class User
    {
        public string UserName { get; set; }
        public string EmailAddress { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            User u = new User()
            {
                UserName = "maor",
                EmailAddress = "maor@example.com"
            };
        }
    }
}

The DebuggerDisplay attribute has a single argument, which is a string to be displayed in the value column for instances of the type. This string can contain braces ({ and }). Text within a pair of braces will be evaluated as a field, property or method.

And the result is:

DebuggerDisplay

Tags: , ,