Viewing collections with IEnumerable Visualizer

How to easily view .NET collections with IEnumerable Debugger Visualizer

Home DailyDrop

Daily Knowledge Drop

When coding a system which makes use of collections of data (even reasonably small collections), inspecting, debugging and discovering issues with the dataset can be very difficult and cumbersome.

The IEnumerable Visualizer, available in Visual Studio 17.2, (Visual Studio 2022), aims to make this tedious process easier allowing for easier inspection of collections of data in Visual Studio.


Watch window

When debugging, the starting point for viewing the data being operated on, is the Watch window.

Watch Window

The issue with the Watch window, is when working with collections, its difficult to find a specific record. There is no useful identifying information immediately available without expanding each row one by one to find the relevant record. This is just not sustainable as the collection size grows.

In addition, there is no way to export the collection of information so that it would be inspected in another tool (such as Excel, for example).


Watch and ToString

One method for making it easier to find the relevent record(s), is to overwrite the ToString method of the object - in this example, the Song object.

public class Song
{
    public int Id { get; set; } 

    public string Name { get; set; }

    public string Artist { get; set; }

    public int YearReleased { get; set; }

    public int LengthInSeconds { get; set; }

    public override string ToString()
    {
        return $"Song `{Name}` by '{Artist} released " +
            $"in '{YearReleased}' and is '{LengthInSeconds}' seconds long";
    }
}

The ouput of the ToString method, pulls through to the Watch window:

Watch Window with ToString

At least now one has a visual cue as to the contents of each record without having to expand the row in the window. However, it still requires inspecting each row, which is not sustainable with thousands of rows. Also, it might not be possible to add a ToString method if the entity is owned by a third party.


IEnumerable Visualizer

The IEnumberable Visualizer solves many of the issues (but not all) - this window is available by clicking the magnifying glass for the collection, in the Watch window:

IEnumberable Visualizer location

This icon will bring up the IEnumberable Visualizer:

IEnumberable Visualizer

This windows allows for the data to be:

  • Sorted by any column
  • Exported to Excel

This will greatly increase the ability to:

  • Find the relevent record
  • Perform additional operations on the data if required (sums, filtering) via exporting

Notes

Having worked with large collections before, this new visualizer is definitely going ease the pain and difficulty collection inspection brings! There are two future enhancements I feel would add great value to the already useful feature:

  • A column in the visualizer which indicates the index of the record in the underlying collection. This will make it easier to find the record in the Watch window to perform updates on the data
  • The ability to filter data in the visualizer, making it even easier to find and search for data.

References

View .NET collections with the new IEnumerable Debugger Visualizer
Debugging collections

Daily Drop 91: 08-06-2022

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net visualstudio debugging collections