IEnumerable Visualizer In Visual Studio 2022

Visual Studio 2022 17.2 shipped the other day, and in it was a handy little feature that I can definitely see myself using a lot going forward. That is the IEnumerable Visualizer! But before I dig into what it does (And really it’s quite simple), I wanted to quickly talk about why it was so desperately needed.

Let’s imagine I have a class like so :

class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

And somewhere in my code, I have a List of people with a breakpoint on it. Essentially, I want to quickly check the contents of this list and make sure while debugging that I have the right data in the right place.

Our first port of call might be to simply is do the “Hover Results View” method like so :

But… As we can see it doesn’t exactly help us to view the contents easily. We can either then go and open up each item individually, or in some cases we can override the ToString method. Neither of which may be preferable or even possible depending on our situation.

We can of course use the “Immediate Window” to run queries against our list if we know we need to find something in particular. Something like :

? people.Where(x => x.FirstName == "John")

Again, it’s very adhoc and doesn’t give us a great view of the data itself, just whether something exists or not.

Next, we can use the Autos/Watch/Locals menu which does have some nice pinning features now, but again, is a tree view and so it’s hard to scroll through large pieces of data easily. Especially if we are trying to compare multiple properties at once.

But now (Again, you require Visual Studio 2022 17.2), notice how in the Autos view we have a little icon called “View” right at the top of the list there. Click that and…

This is the new IEnumerable visualizer! A nice tabular view of the data, that you can even export to excel if you really need to. While it’s a simple addition and really barebones, it’s something that will see immediate use in being able to debug your collections more accurately.

ENJOY THIS POST?
Join over 3,000 subscribers who are receiving our weekly post digest, a roundup of this weeks blog posts.
We hate spam. Your email address will not be sold or shared with anyone else.

3 comments

    1. how is it mire effective? it requires yiu to proactively select the properties you want to see (+you need to do it before you execute the debug session). Here you get a clear visualization of all the attributes (+the option to copy all the data, sorting it)

  1. “We can either then go and open up each item individually, or in some cases we can override the ToString method.”

    You should never override ToString for debugging purposes. ToString should be reserved for its intended purpose… to produce a string representation of the object for the end user. The developer debugging is not the end user. It’s possible there is a string representation wanted for the end user that’s also useful for the developer while debugging, but the purposes and intents are NOT the same. For debugging purposes you should use the DebuggerDisplayAttribute instead. Though like you said, this may not be possible for you to do, and so this visualizer is a great addition to the IDE.

Leave a Reply

Your email address will not be published.