Using DebuggerBrowsable Attribute for a better view in debugger window

There are lots of small tricks in .net which can be very useful if used wisely. In this post I want to discuss the use of DebuggerBrowsableAttribute. Using this attribute you can control how a member of class will be displayed in debugger windows during debugging. Before that consider small example:

public class Organization
{
    public List<Employee> Employees
    {
        get;
        set;
    }

    public Organization()
    {
        this.Employees = new List<Employee>();
    }
}

 

public class Employee
{
    private Guid UniqueGuid
    {
        get;
        set;
    }

    public string Code
    {
        get;
        set;
    }

    public string Name
    {
        get;
        set;
    }

    public Employee()
    {
        UniqueGuid = Guid.NewGuid();
    }
}

Lets create a instance of Organization and add some sample data:

Organization org = new Organization();
org.Employees.Add(new Employee() { Code = "E01", Name = "Matt" });
org.Employees.Add(new Employee() { Code = "E02", Name = "Tim" });
org.Employees.Add(new Employee() { Code = "E03", Name = "David" });
org.Employees.Add(new Employee() { Code = "E04", Name = "Tom" });

Now when you run this program in debug mode and try to view the org in debugger window it will look like this:

Lets try to change it using DebuggerBrowsableAttribute. Make following changes by adding DebuggerBrowsableAttribute:

    [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
    public List<Employee> Employees
    {
        get;
        set;
    }


    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private Guid UniqueGuid
    {
        get;
        set;
    }

Again run this program in debug mode and try to view the org in debugger window. Now it will look like this

You can see how this small but useful attribue can change the look of the debugger window.

8 comment(S)


Visual C# Kicks on Jan 27, 2010 01:14 PM

Talk about useful

moleskin on Jul 26, 2011 02:11 AM

Add the DebuggerNonUserCode attribute above the 'get' and the 'set', and forget about the field. The debugger won't even step into the property code.
Don't forget to turn on the 'Just my code' feature in the VS Debugger settings.

reverse phone on Nov 17, 2011 11:33 AM

I tell you! The things you can learn just by browsing the Internet! Content like what is found on this website is not easily found - and when it is, it needs to be recognized as such.

qbtbrmjb on Feb 25, 2012 09:46 AM


ptc on Mar 12, 2012 04:25 AM

I really appreciate your efforts and I will be waiting for your further write ups thanks once again. you have a quite exceptional view.

Aklapothik on Mar 20, 2012 03:18 AM

thanks for the discussion ,its really helpful for my work.

florida gator forum on Apr 1, 2012 06:09 AM

A Florida Gator sports blog that covers every game, roster, exclusive player interviews, and much more. It is your source for Florida Gator news. Come check out the forum and discuss Heisman trophy winner Tim Tebow and the rest of the Gators.

Darlene Tysinger on Jun 1, 2012 10:28 PM

How to Keep A Boyfriend Advice for Women on Men & Relationships How to Keep a Boyfriend is an online resource for women on relationships and understanding men.Get tips on what men want in a woman that will help bring you the confidence you need to be secure in your relationship. Find new ways to bring out the romantic and passionate side of your man.

Leave a comment