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

Similar Posts:

Tags: , ,



5 Comments to “DebuggerDisplay Attribute”

  1. maor's blog : DebuggerDisplay Attribute | June 3rd, 2009 at 22:56

    [...] Read full article Posted: Wednesday, June 03, 2009 10:56 PM by Maor David-Pur Filed under: Visual Studio, Tips [...]

  2. How to Make Thousands of Dollars Posting Links on Google | June 10th, 2009 at 21:52

    Thanks for posting, I’ll definitely be subscribing to your blog.

  3. Kelly Brown | June 12th, 2009 at 21:01

    The best information i have found exactly here. Keep going Thank you

  4. KattyBlackyard | June 15th, 2009 at 04:38

    Hi, gr8 post thanks for posting. Information is useful!

  5. Grapefruit Seed Extract | February 3rd, 2010 at 04:19

    how long have you been writing this blog? I wish I found it earlier :D

Leave a Comment