Possible Duplicate:
Is there an alternative to Dictionary/SortedList that allows duplicates?
I am looking for a Dictionary sort of class which can have duplicate Keys.
I search about it, and found LookUp class can use to store duplicate keys, but It has no default constructor, So we can't initialize it without any other object to LookUp.
But I have no any such object initially from which I can initialize a LookUp object.
So, My question is, Is there any class in .Net framework 3.5, which behave like Dictionary but allow me to have duplicate keys like LookUp?
[C#] Dictionary with duplicate keys The Key value of a Dictionary is unique and doesn't let you add a duplicate key entry. To accomplish the need of duplicates keys, i used a List of type KeyValuePair<> .
Python dictionary doesn't allow key to be repeated.
You could create a list of key value pairs.
List<KeyValuePair<string,int>>
A Dictionary, by definition, will never be able to have multiple keys with the same value. (If you looked up a key whatever would you return?) Even a Lookup, which you refer to, doesn't allow it. What you can do is have each key refer to multiple values (logically, not technically). This is done by having a dictionary in which the value is a data structure of some sort (for example, a List
) that contains all of the values that correspond to that particular key.
You can compose a type yourself by using a dictionary of lists, Dictionary<TKey, List<TValue>>
You can create a class inheriting from that class and add suitable add-methods etc that handles creating a new list for the first item on a given key.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With