Posted by: Cirilo Meggiolaro | 02/24/2009

Tip of the day #133 – DebuggerDisplay attribute

Tip #132 described how to use the DebuggerBrowsable attribute to control which items are displayed and how collections are displayed on the Watch Window.

Let’s check today another attribute called DebuggerDisplay that gives us the possibility to change the information displayed on the value column of the Watch Window.

The DebuggerDisplay attribute may be used for classes, structures, delegates, enumerations, fields, properties or assemblies.

Basically you may use any of the object properties to be displayed. The value is a free text that you can type anything and create placeholders for the values when typing the property name between curly brackets. In debug mode the placeholders are going to be replaced with the property value.

How to…

The following code snippets show some examples on how to use the attribute and the respective ouput to the Watch Window based on a class called Customer:

[DebuggerDisplay(“Customer Instance”)]
public class Customer { }

Picture 1 - Debugger Display attribute example 1 output

Picture 1 - Debugger Display attribute example 1 output with a literal text.

[DebuggerDisplay(“Customer ID: {ID} – Customer Name: {Name}”)]
public class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }
    public DateTime DOB { get; set; }
}

Picture 2 - Debugger Display attribute example 2 output

Picture 2 - Debugger Display attribute example 2 output with properties values.

[DebuggerDisplay(“Total of orders: {Orders.Count}”)]
public class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }
    public DateTime DOB { get; set; }
    public List<Order> Orders { get; set; }
}

Picture 3 - Debugger Display attribute example 3 output

Picture 3 - Debugger Display attribute example 3 output using a property from a collection.


Responses

  1. that’s really useful. Thanks a lot!


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: