In many cases while writing software we fail to think about the users of our code. This is often the case when it comes to debugging since most of use feel it’s easy enough to just jump into the code and see what’s going on. While this is possible it’s the simple things in life that sometimes make the biggest difference. Take for instance the case of debugging some code you didn’t write. In most cases if you look at a type in the locals window all you will see is a ToString() of the type (see Figure 1) which is not very helpful. To lean more about the type you are forced to drill down into the type looking at the properties to see what’s going on. Wouldn’t it be nice to see some high level values instead of the ToString() of type? If we take this even further wouldn’t it be nice to see some values that the designer of the type thought would be important if you were in a debug session. This is where the DebuggerDisplayAttribute come into play.
Figure 1: Debug Locals Window (click to enlarge)
The DebuggerDisplayAttribute determines how a class or field is displayed in the debugger window eliminating the ToString() default you normally see. Take for instance the code in Listing 1. This is the version of SimpleClass that resulted in the locals window view shown in Figure 1 which as we said was not very helpful. The code in Listing 2 shows how we can add the DebuggerDisplayAttribute to the class to see some more interesting information. In this case we added the attribute with the value we wanted to show up in the value column of the debugger window (see Figure 2). The format “Some Interesting Data = {_hiddenInternalInfo}” consists of two parts: (1) the text to be display and (2) the value from the class {_hiddenInternalInfo} we wanted to see. You are not limited to just one value. If we wanted to see the MyProp1 value as well we could change the entry to look like this “Some Interesting Data = {_hiddenInternalInfo}, MyProp1 = {MyProp1}” and get a result that looks like Figure 3. As you can see it’s not really hard to add some additional information to the locals window and help someone who is using your code figure out what’s important.
class SimpleClass { private string _hiddenInternalInfo; public string MyProp1 { get; set; } public string MyProp2 { get; set; } public SimpleClass() { this._hiddenInternalInfo = "Hidden data"; this.MyProp1 = "My intitial data for MyProp1"; this.MyProp2 = "My intial data for MyProp2"; } }
Listing 1: SimpleClass with no attribute
[DebuggerDisplay("Some Interesting Data = {_hiddenInternalInfo}")] class SimpleClass { private string _hiddenInternalInfo; public string MyProp1 { get; set; } public string MyProp2 { get; set; } public SimpleClass() { this._hiddenInternalInfo = "Hidden data"; this.MyProp1 = "My intitial data for MyProp1"; this.MyProp2 = "My intial data for MyProp2"; } }
Listing 2: SimpleClass with DebuggerDisplayAttribute
Figure 2: Debug Locals Window with More Information (click to enlarge)
Figure 3: Debug Locals Window with a List of Information (click to enlarge)