Duplicate keys in .NET dictionaries?
The List class actually works quite well for key/value collections containing duplicates where you would like to iterate over the collection. Example:
List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
// add some values to the collection here
for (int i = 0; i < list.Count; i++)
{
Print(list[i].Key, list[i].Value);
}
If you're using .NET 3.5, use the Lookup
class.
EDIT: You generally create a Lookup
using Enumerable.ToLookup
. This does assume that you don't need to change it afterwards - but I typically find that's good enough.
If that doesn't work for you, I don't think there's anything in the framework which will help - and using the dictionary is as good as it gets :(