DailyCoding > General

Using DebuggerBrowsable Attribute for a better view in debugger window

Describe how we can DebuggerBrowsable attribute for customizing the debugger window view.
Author admin on Jan 23, 2010 3 Comments
Rate it    (Rated 4 by 6 people)
4,287 Views

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.

C# | Utility
 

Discussion

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.

Leave a Comment

Name
Email Address
Web Site