Quantcast
Channel: Category Name
Viewing all articles
Browse latest Browse all 5971

Customize object displays in the Visual Studio debugger YOUR way

$
0
0

Have you ever stared at objects in a debugger window and wished that you could view those objects by something other than their type?  I certainly have!  Expanding items to determine each one’s identity can become tiresome very fast. Ideally, it would be great to quickly locate them by a particular property value.  Luckily for us, Visual Studio has two not-so-well-known attributes known as DebuggerDisplay for managed users, and Natvis for native C++ users. These attributes let you customize how you view objects in debugger windows such as the Watch, Autos, Locals, and datatips!

Locals and DataTips windows with and without DebuggerDisplay attribute appended to code
Figure 1 – Locals and DataTips windows with and without DebuggerDisplay attribute appended to code

What is the DebuggerDisplay attribute?

By writing DebuggerDisplay syntax at the top of a class, you can choose what strings and properties you want at the top of each object node in debugger windows.  Besides displaying strings in debugger windows, adding curly brackets ({}) to the DebuggerDisplay attribute allows Visual Studio to display the value of a property or method that you specify. You can also add format specifiers to DebuggerDisplay in order to further change how values are displayed and formatted in the debugger windows. In Figure 2, DebuggerDisplay appends the format specifier “nq” (no quotes).  The resulting display shows the string property Title without the surrounding quotation marks.

Basic DebuggerDisplay syntax added to top of Book class
Figure 2 – Basic DebuggerDisplay syntax added to top of Book class

 

Locals window with above DebuggerDisplay syntax added to code
Figure 3 – Locals window with above DebuggerDisplay syntax added to code

 

One previous workaround for performing this task is overriding a class’s ToString() method.  In contrast, DebuggerDisplay controls how an item is displayed without overriding that method.  So, if you don’t want debugging-related content in your ToString() method (especially when that method is called in your actual program), DebuggerDisplay is the way to go!

 

Can I display expressions for each object in debugger windows?

There may be times when you want to display expressions in debugger windows.  Good news: you can display expressions using the DebuggerDisplay attribute!

 

Example of DebuggerDisplay attribute containing an expression
Figure 4 – Example of DebuggerDisplay attribute containing an expression

 

Locals window with above DebuggerDisplay syntax and added expression evaluation
Figure 5 – Locals window with above DebuggerDisplay syntax and added expression evaluation

 

Bad news: DebuggerDisplay expressions can cause additional issues when debugging your code. Potential issues include performance hits for large or complex expressions, compilation and runtime errors when the expression’s language differs from the language being debugged, and application state changes when expressions mutate properties.

 

Figure 6 - DebuggerDisplay attribute with Visual Basic-style ternary expression syntax added
Figure 6 – DebuggerDisplay attribute with Visual Basic-style ternary expression syntax added

 

Figure 7 - Runtime error received after using above Visual Basic-style syntax while debugging in C#
Figure 7 – Runtime error received after using above Visual Basic-style syntax while debugging in C#

 

But fear not! One way to reduce these potential issues with expressions is by creating a private property or method that returns the string of an executed expression and telling DebuggerDisplay to display that property.

 

Figure 8 - Creating a private property containing more complex expressions and formatting referenced by DebuggerDisplay
Figure 8 – Creating a private property containing more complex expressions and formatting referenced by DebuggerDisplay

 

Figure 9 - Creating a method containing more complex expressions and formatting referenced by DebuggerDisplay
Figure 9 – Creating a method containing more complex expressions and formatting referenced by DebuggerDisplay

 

What is the feature equivalent  to DebuggerDisplay for C++ users?

DebuggerDisplay is compatible with C#, F#, and Visual Basic, but if you’re debugging in C++, Natvis is a great alternative!  Though not as simple as adding syntax to the top of a class like DebuggerDisplay, adding a .natvis file to a project lets you customize how objects are displayed.

 

Figure 10 - Example of Natvis being used in Locals window
Figure 10 – Example of Natvis being used in Locals window

 

Right-click the C++ project node in Solution Explorer, select Add > New Item, and select Visual C++ > Utility > Debugger visualization file (.natvis).  The result is an XML file where you can control which properties are displayed while debugging.

 

Figure 11 - Example Natvis file corresponding to display shown above
Figure 11 – Example Natvis file corresponding to display shown above

 

To learn more about using Natvis while debugging C++ projects, check out the documentation.

 

These features are awesome and will save me lots of time!  How can I help share DebuggerDisplay and Natvis with others?

Fun fact: both DebuggerDisplay and Natvis attributes have been in Visual Studio for years!  These attributes are extremely useful to most developers but are still not as discoverable and well-known as they could be.  As a result, we are currently working to provide an easier method to discover these attributes better, and your feedback will help make this happen!  Please complete this survey which will give us insight in providing an improved experience when using these attributes.

The post Customize object displays in the Visual Studio debugger YOUR way appeared first on The Visual Studio Blog.


Viewing all articles
Browse latest Browse all 5971

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>