views:

83

answers:

3

When we must use DebuggerDisplay Attributes? What is the advantage of using this?

+4  A: 

This article explains it well. You can use this attribute on your classes to display a more meaningful text when you debug. For example: Suppose you have a the following class:

[DebuggerDisplay("x = {x} y = {y}")]    
MyClass
{
    private int x;
    private int y;
    ...
}

Once you debug an instance of MyClass in the Visual Studio debugger and you hover over it (or put it in the Watch Window, you no longer see "MyClass" there but instead "x = 4 y = 5" (assuming that x and y of this instance currently have this value. This is just an example you can do much more as the article explains it.

bitbonk
+1  A: 

The DebuggerDisplay attribute sets how the class or field is displayed when you view the class or field in the debugger.

For example, rather than see that the variable of is type Address, you could get the debugger to actually display the address that the object is storing.

It just makes debugging a little bit easier! When debugging is easier - the life of a programmer becomes a lot more enjoyable! ;-)

Mongus Pong
+1  A: 

To answer your question when you should use it, my recommendation is on every single class that is a business object that has data properties that have meaning. Especially important is for any class that will be inside a collection. Since classes that are inside a collection when you expand the results view you'll only see the fully qualified type name and will have to expand each result individually to see which item it is.

However when you use the DebuggerDisplay attribute you can see the properties you deem most important right in the results view of the quick watch window when debugging code that contains collections.

Chris Marisic