[C#] Dictionary with duplicate keys

Sometimes there is a need to create a Dictionary with duplicates 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<>.

For example if you have to add a string with multiple values:

List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
list.Add(new KeyValuePair<string, string>("Name1", "Phone Num1"));
list.Add(new KeyValuePair<string, string>("Name1", "Phone Num2"));

To test the content of the List:

foreach (KeyValuePair><string, string> item in list)
{
    Console.Write(item.Key + "=>" + item.Value);
}

Robin

Software Engineer / Application Developer

Subscribe to robsCode

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!