Tom Goff's .Net Musings

Tidbits of information regarding .Net, C#, and SQL Server.

COTW: DebuggerBrowsableAttribute Class

with one comment

The DebuggerBrowsableAttribute[^] class allows you to specify which fields and properties of your classes will be displayed in the debugger view (e.g. Visual Studio’s Watch windows). The following code samples and screen-shots will illustrate how to use this attribute.

Note: This attribute is not currently supported in Visual Basic.

There are three possible settings that can be used with this attribute and they are defined by the DebuggerBrowsableState[^] enumeration. The first option is Collapsed, which is the default value if the DebuggerBrowsableAttribute is not specified. In the following class, neither the field nor the property have this attribute specified. This means it will use the Collapsed option.

public class MyClass {
    /// <summary>
    /// Initializes a new instance of the
    /// <see cref="MyClass"/> class.
    /// </summary>
    public MyClass() {
        this.items = new List<String>();
        this.items.Add("One");
        this.items.Add("Two");
        this.items.Add("Three");
    }

    /// <summary>
    /// Holds the items.
    /// </summary>
    private List<String> items;

    /// <summary>
    /// Gets or sets the items.
    /// </summary>
    /// <value>The items.</value>
    public List<String> Items {
        get {
            return this.items;
        }
        set {
            this.items = value;
        }
    }
}

When viewing an instance of this class in Visual Studio’s watch window, it will appear as shown here:

DebuggerBrowsableAttribute_Collapsed

The next option is called Never and will remove the associated field or property from the debugger’s view. In the following code, we have indicated that we do not want the debugger to display the Items property:

public class MyClass {
    /// <summary>
    /// Initializes a new instance of the
    /// <see cref="MyClass"/> class.
    /// </summary>
    public MyClass() {
        this.items = new List<String>();
        this.items.Add("One");
        this.items.Add("Two");
        this.items.Add("Three");
    }

    /// <summary>
    /// Holds the items.
    /// </summary>
    private List<String> items;

    /// <summary>
    /// Gets or sets the items.
    /// </summary>
    /// <value>The items.</value>
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    public List<String> Items {
        get {
            return this.items;
        }
        set {
            this.items = value;
        }
    }
}

As you can see below, the Items property is no longer shown in the watch window:

DebuggerBrowsableAttribute_Never

The final option is called RootHidden and is only useful for fields or properties that are collections or arrays. If this option is used for items that are not a collection or an array, then it will be hidden from the debugger’s view. The screen-shot below illustrates how our class would be displayed if we specified the RootHidden option for the Items property.

DebuggerBrowsableAttribute_RootHidden

As you can see, the entries that would typically be found below the Items property (by expanding the plus sign) are now merged with the other MyClass fields and properties. The RootHidden option is useful if you are writing a class that wraps a collection or array. When viewing such a class in the debugger, then you would typically drill down to the collection or array. Using this option would save you that additional step when debugging.

If you are developing .Net classes or controls for use by other developers, then you should consider using the Never option to hide private and internal fields and properties. Since the consumers of your .Net assemblies would not be able to access or modify private or internal members, then typically they would not need to see them in the debugger. Of course, there are always exceptions.

Keep in mind that these attributes are just recommendations of how you think your class should be shown in the debugger. The debugger does not have to follow them. In Visual Studio, you can choose to ignore this (and other similar) attributes by checking the configuration option shown below.

DebuggerBrowsableAttribute_VS

As with last week, this article[^] is a great resource for further reading on this subject.

Advertisements

Written by Tom

October 23, 2007 at 7:17 am

One Response

Subscribe to comments with RSS.

  1. Brilliant! I found out why my DebuggerDisplaAttributes were not working any more (rashly selecting “Show Raw Structure of Objects…” option at some point).

    Thank you!

    Nij

    August 19, 2010 at 2:03 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: