The DebuggerDisplay attribute can help us customizing  output in the debugger windows.

The first way that we can start to take some control of the debugger output is to override the ToString method. By default, Visual Studio will use this overridden ToString in the debugger.

The other option we have is to use the DebuggerDisplay attribute. We can see the attribute here applied at the class level. When you use the attribute, you supply a string. Inside this string, you can reference the members inside the class. To do this, you enclose the name of the member in curly braces. For example, this refers to the First Name and Last Name properties from the orders user. Let’s take a look at this in the debugger

If we debug this version now, and hover over the order, we can see that the root level now uses the attribute at the class level.

The line:

I am text block. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

So, when would you use the DebuggerDisplay attribute?

You probably wouldn’t use it all the time as it does introduce a maintenance overhead but at the class level it can be really useful, if you haven’t overridden the ToString method. If you have some kind of complex data, you may find it useful to use. Regardless, whenever you use the DebuggerDisplay attribute, you should make sure that it actually does increase the clarity at debug time rather than detract from it.

LEAVE A REPLY

Please enter your comment!
Please enter your name here