Customized Debugger Tooltip

Code-Execute-Debug-Code-Execute-Debug..

This seems to be never ending cycle for any developer. Considerable amount of time of this cycle is spend in Debug phase, wherein user steps through breakpoints and traverse the objects and their properties.

Thankfully, Microsoft has ensured that primitive types always shows the value, instead of any less-meaningful information.

Image

But what happens when you attempt to hover over instance of a user-defined class ? The pop-up shows the type of class.

Image(1)

Waiting for the pop-up to appear and then using the ‘+’ to traverse the object to see its properties can be a cumbersome task, especially when you are ‘mostly’ interested in one or two properties. Many a times, you would pull you could control what the debugger displays.

This is where DebuggerDisplay  attribute comes into play. Of course, you could always use ‘ToString()’ for the same purpose, but lets say, ‘ToString()’ should be reserved for more meaningful jobs.

All you need to do is to decorate the class with DebuggerDisplay Attribute, pass a string with property name from the class (within curly braces) and you are all set to have debugger tool-tips of your choice.

For example, in my example Class Student, I have different properties like FirstName, LastName, Age among others. But mostly, I am only interested to know the full name of the student while debugging. As you can see from the example below, I have decorated the class with DebuggerDisplay attribute, and passed an expression ( Yes – You heard it right, you can use expression as well) consisting of a combination of FirstName and LastName.

Image(2)

Voila !! That’s at it. Check what happens now when you debug.

Image(3)

Simple, but interesting isn’t it ?.

Advertisements