Click here to Skip to main content
14,029,295 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

6.5K views
11 bookmarked
Posted 24 Mar 2019
Licenced CPOL

Visual Studio Tips - DebuggerDisplay

, 24 Mar 2019
Rate this:
Please Sign up or sign in to vote.
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:

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).

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)

Share

About the Author

Jeremy Hutchinson
Software Developer
United States United States
I develop applications using Angular, ASP.Net MVC and SQL hosted on AWS by day, and in my free time I develop apps for Windows Phone and Windows 10 as well as web apps hosted in Azure. I always try to blog about what I've learned.

I also help run the Casco Bay .Net User Group

You may also be interested in...

Comments and Discussions

 
General'nq' literally means 'no quotes' (strip leading quotes from object value). Pin
DuanChengHua27-Mar-19 18:54
memberDuanChengHua27-Mar-19 18:54 
Questionnq Pin
Member 1208887227-Mar-19 5:57
memberMember 1208887227-Mar-19 5:57 
AnswerRe: nq Pin
CodeHawkz27-Mar-19 18:51
memberCodeHawkz27-Mar-19 18:51 
QuestionAlternative Pin
Klaus Luedenscheidt25-Mar-19 19:47
memberKlaus Luedenscheidt25-Mar-19 19:47 
AnswerRe: Alternative Pin
mdcclxv9-Apr-19 10:35
membermdcclxv9-Apr-19 10:35 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190419.4 | Last Updated 25 Mar 2019
Article Copyright 2019 by Jeremy Hutchinson
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid