Duplicate Key Dictionary

Possible duplicate:
Is there an alternative to the / SortedList dictionary that allows duplication?

I am looking for a dictionary class that can have duplicate keys.

I am looking for this and found that the LookUp class can be used to store duplicate keys, but it does not have a default constructor, so we cannot initialize it without any other LookUp objects.

But I do not have such an object from which I can initialize the LookUp object.

So my question is: is there any class in the .Net framework 3.5 that behaves like a dictionary but allows me to have duplicate keys like LookUp?

+8
source share
3 answers

A dictionary, by definition, can never have multiple keys with the same value. (If you looked at the key, what would you not return?) Even the Lookup you are talking about does not allow it. What you can do is that each key refers to several values ​​(logically, not technically). This is done using a dictionary in which the value is a data structure (for example, a List ) that contains all the values ​​that correspond to this particular key.

+12
source share

You can create a list of key value pairs.

 List<KeyValuePair<string,int>> 
+19
source share

You can create the type yourself using the dictionary of lists, Dictionary<TKey, List<TValue>>

You can create a class that inherits from this class and add suitable add-methods, etc., which handles the creation of a new list for the first element on a given key.

+3
source share

All Articles