Click here to Skip to main content
15,844,676 members
Articles / Programming Languages / C#

Visual Studio Tips - DebuggerDisplay

Rate me:
Please Sign up or sign in to vote.
4.93/5 (41 votes)
6 Apr 2021CPOL 14.7K   31   8
Visual Studio tips - DebuggerDisplay

When you look at an object in the Watch window, what you see is whatever comes out of the ToString() method. But what if you could control what was displayed so that you could see some meaningful value? Well, you can.

If we have a Person class defined like this:

C#
namespace VSTips.DebuggerDisplay
{
    class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

If we create an instance and look at it in the watch window, all we see is {VSTips.DebuggerDisplay.Person}. We can, of course, drill in to see the individual properties, and that isn’t so bad when you’re looking at a single object, but when you look at a List<Person> and see this, you know you’re going to spend a lot of time clicking to find the object you’re looking for.

alt text

If we go back to the definition of the Person class and add an attribute, we can make the watch window display whatever we want. In this case, we’ll display the last name and the first 5 characters of the first name (taking the first 5 just to show the flexibility).

C#
using System.Diagnostics;

namespace VSTips.DebuggerDisplay
{
   [DebuggerDisplay("{LastName,nq}, 
            {FirstName.Length >= 5 ? FirstName.Substring(0, 5) : FirstName,nq}")]
    class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

When you look at the watch list with this Debugger Display, what you see is much more helpful.

alt text

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I’m a Software Engineer at Microsoft working on the Azure Portal. Before that I spent about 20 years developed various business applications at a number of different companies. I have a passion for writing clean, scalable code and sharing what I’ve learned with others.

I also help run the Casco Bay .Net User Group

Comments and Discussions

 
GeneralMy vote of 5 Pin
tal_segal10-Apr-21 2:52
tal_segal10-Apr-21 2:52 
PraiseMy vote is 5 Pin
AchLog8-Apr-21 0:43
AchLog8-Apr-21 0:43 
General'nq' literally means 'no quotes' (strip leading quotes from object value). Pin
DuanChengHua27-Mar-19 19:54
DuanChengHua27-Mar-19 19:54 
'nq' literally means 'no quotes' (strip leading quotes from object value).

Questionnq Pin
Member 1208887227-Mar-19 6:57
Member 1208887227-Mar-19 6:57 
AnswerRe: nq Pin
CodeHawkz27-Mar-19 19:51
CodeHawkz27-Mar-19 19:51 
QuestionAlternative Pin
Klaus Luedenscheidt25-Mar-19 20:47
Klaus Luedenscheidt25-Mar-19 20:47 
AnswerRe: Alternative Pin
mdcclxv9-Apr-19 11:35
mdcclxv9-Apr-19 11:35 
GeneralRe: Alternative Pin
L Hills7-Apr-21 23:48
L Hills7-Apr-21 23:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.