You have to write your own, just like the authors of the immediate window did, but the good news is that it isn't that hard.
All we have to do is create a string that says "Count = "
followed by the Count
, then we can output each item using the override of Select
that includes the index
, so we can format the output with both the index and value for each item.
We can even make it an extension method, so it becomes part of the Values
property:
public static class Extensions
{
public static string GetDetailedString<TKey, TValue>(
this Dictionary<TKey, TValue>.ValueCollection input)
{
var output = $"Count = {input.Count}";
if (input.Count > 0)
output += Environment.NewLine + " " +
string.Join(Environment.NewLine + " ",
input.Select((item, index) => $"[{index}]: \"{item}\""));
return output;
}
}
If you want to extend this to the KeyCollection
as well, then we might as well extend it to List<T>
and create a few overloads. Heck, while we're at it, we may as well include an overload for the Dictionary
itself:
public static class Extensions
{
public static string GetDetailedString<TKey, TValue>(
this Dictionary<TKey, TValue>.KeyCollection keys)
{
return keys.ToList().GetDetailedString();
}
public static string GetDetailedString<TKey, TValue>(
this Dictionary<TKey, TValue>.ValueCollection values)
{
return values.ToList().GetDetailedString();
}
public static string GetDetailedString<T>(this IList<T> input)
{
var output = $"Count = {input.Count}";
if (input.Count > 0)
output += Environment.NewLine + " " +
string.Join(Environment.NewLine + " ",
input.Select((item, index) => $"[{index}]: \"{item}\""));
return output;
}
public static string GetDetailedString<TKey, TValue>(
this Dictionary<TKey, TValue> dict)
{
var output = $"Count = {dict.Count}";
if (dict.Count > 0)
output += Environment.NewLine + " " +
string.Join(Environment.NewLine + " ",
dict.Select((item, index) =>
$"[{index}]: {{[{item.Key}, {item.Value}]}}"));
return output;
}
}
In use this might look something like:
Dictionary<string, string> _dict = new Dictionary<string, string>
{
{ "key1", "value1"},
{ "key2", "value2"},
{ "key3", "value3"},
};
Console.WriteLine("Dictionary:");
Console.WriteLine(_dict.GetDetailedString());
Console.WriteLine("\r\nKeys:");
Console.WriteLine(_dict.Keys.GetDetailedString());
Console.WriteLine("\r\nValues:");
Console.WriteLine(_dict.Values.GetDetailedString());
Output
