Skip to content
Bruno Sonnino
Menu
  • Home
  • About
Menu

IEnumerable Visualizer in Visual Studio

Posted on 4 June 2022

One thing that bothers me when debugging an app in Visual Studio is to get the values of an IEnumerable. For example, when you have a code like this one:

    var people = new List<Person>();
    for (int i = 1; i <= 100; i++)
    {
        people.Add(new Person { Name = $"Name {i}", Address = $"Address {i}" });
    }
    Console.WriteLine(people);

    class Person
    {
        public string? Name { get; init; }
        public string? Address { get; init; }
    }
C#

If you run it and put a breakpoint in the Console.WriteLine(people), you will get something like this:


This is less than optimal, you have to drill down the list to get the values. If you are searching for a specific value, things get worse. Open each value and take a look if it’s the right one can be a nightmare.

You can improve this experience a lot by using a tool like OzCode (https://oz-code.com/) – take a look at my article at https://blog.revolution.com.br/2016/12/02/debugging-with-ozcode/, but this tool is not free.

One other way to improve the experience is to override the ToString method and show something more useful:

    var people = new List<Person>();
    for (int i = 1; i <= 100; i++)
    {
        people.Add(new Person { Name = $"Name {i}", Address = $"Address {i}" });
    }
    Console.WriteLine(people);

    class Person
    {
        public string? Name { get; init; }
        public string? Address { get; init; }
        public override string ToString()
        {
            return $"Name: {Name} Address: {Address}";
        }
    }
C#

You can even use the new Records, introduced in C# 9, that implement internally the ToString method:

    var people = new List<Person>();
    for (int i = 1; i <= 100; i++)
    {
        people.Add(new Person($"Name {i}", $"Address {i}"));
    }
    Console.WriteLine(people);

    record Person(string Name, string Address);
C#

But filtering, searching and scrolling through long lists is still cumbersome.

In the Preview Version of Visual Studio 2022, Microsoft introduced a new feature that will improve the debugging experience a lot: the IEnumerable Visualizer.

With this tool (it’s still only in the Preview version, it should came to the production version in one of the next updates), you can get an improved experience for debugging IEnumerables. You can click on the View button in the tooltip and Visual Studio will open a new window with the data:

You can sort the data by clicking on the columns titles and, if you want to work with the data, you can click on the Export to CSV/Export to Excel button and open the results in Excel, so you can work with them. Export to CSV will save the data to a CSV file and Export to Excel will open Excel with the data in a new workbook. Pretty cool, no?

While this is not a full fledged visualizer, it’s still in preview and it’s a work in progress, with room for lots of improvements, like searching and filtering data. But it’s a cool start and a welcome improvement.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • May 2025
  • December 2024
  • October 2024
  • August 2024
  • July 2024
  • June 2024
  • November 2023
  • October 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • April 2020
  • March 2020
  • January 2020
  • November 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • June 2017
  • May 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • October 2015
  • August 2013
  • May 2013
  • February 2012
  • January 2012
  • April 2011
  • March 2011
  • December 2010
  • November 2009
  • June 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • July 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • Development
  • English
  • Português
  • Uncategorized
  • Windows

.NET AI Algorithms asp.NET Backup C# Debugging Delphi Dependency Injection Desktop Bridge Desktop icons Entity Framework JSON Linq Mef Minimal API MVVM NTFS Open Source OpenXML OzCode PowerShell Sensors Silverlight Source Code Generators sql server Surface Dial Testing Tools TypeScript UI Unit Testing UWP Visual Studio VS Code WCF WebView2 WinAppSDK Windows Windows 10 Windows Forms Windows Phone WPF XAML Zip

  • Entries RSS
  • Comments RSS
©2025 Bruno Sonnino | Design: Newspaperly WordPress Theme
Menu
  • Home
  • About