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; }
}
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}";
}
}
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);
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.