Help Outputting List Values

Dictionary<int, List<string>> myDict = new Dictionary<int, List<int>>();

foreach (KeyValuePair <int, int<List>> a in MyDict)
{
Console.Write(string.Format("Key: {0}", a.Key));

foreach (var b in a.Value)
{
Console.Write(string.Format("Value: {0}", b));
}
}​

This Outputs the following:

Key: 1
Value: 324234
Value: 12331
Value: 6575
Value: 45645
Key: 2
Value: 23423
Value: 4646f
Value: etc....

But I can't work out how to do this:

Key: 1
Values: 343243, 4435445, 23324, 454543
Key: 2
Values: 343433, 488445, 27643

Or this:

Key 1, 3243, 324243, 6432
Key2, 3451, 45445, 2344, 76767

If I try to write the Values (ie a.Value) as one it will simply output this:

system.collections.generic.list`1[system.string] write dictionary.

I know I can access indivdual values in each key with:
[Key][4]. etc

But I cannot work out how to Output the values associated with each key as one block. Each key could have a different number of values also, not always the same.

Posted Oct 1, 2024, 9:06 AM UTC

114
Loading comments…