Posted by: Cirilo Meggiolaro | 02/23/2009

Tip of the day #132 – DebuggerBrowsable attribute

Most developers are used to add objects to the Watch Window to examine properties and values of an object. If you have simple objects may be it does not make difference but with complex objects control how the properties and objects that are displayed may really useful.

The attribute DebbugerBrowsable may be applied to fields, properties or indexers to control how it is displayed on the Watch Window. It accepts a DebuggerBrowsableState enumerator item as parameter. The following options are available:

  • Never: Hides the element;
  • Collapsed: Shows the element as collapsed;
  • RootHidden: Do not display the element. If the item is a collection, display the collection items only.

The following code shows a class with some properties and their respective attributes applied to them:

public class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }
    public DateTime DOB { get; set; }

    /// The property is not displayed.
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    public bool Active { get; set; }

    /// Defines that the root property is not displayed
    /// and its items are displayed as an array of items.

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

public class Order
{
    public int OrderID { get; set; }
    public decimal BilledTotal { get; set; }

    /// The property is not displayed.
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    public DateTime Date { get; set; }
}

Output

Executing the code you will be able to view the properties as shown on the following pictures:

Picture 1 - Watch window shows the objecs properties

Picture 1 - Watch window shows the objects properties

Picture 2 - Watch window shows the objects properties and the order items expanded.

Picture 2 - Watch window shows the objects properties and the order items expanded.

Advertisements

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

Categories

%d bloggers like this: