DEV Community

Jeremy Hutchinson
Jeremy Hutchinson

Posted on • Originally published at hutchcodes.net on

1 1

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

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

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay