I'm using C# 4.0. I want to store (string,string) pair using IDictionary. like below:
Dictionary<string, string> _tempDicData = new Dictionary<string, string>();
_tempDicData.Add("Hello", "xyz");
_tempDicData.Add("Hello", "aaa");
_tempDicData.Add("Hello", "qwert");
_tempDicData.Add("Hello", "foo");
_tempDicData.Add("Hello", "pqr");
_tempDicData.Add("Hello", "abc");
but got an error:
An item with the same key has already been added.
So How can I store same key in IDictionary?
A class where u can add duplicated keys may look like the following:
class TDictionary<K, V>
{
struct KeyValuePair
{
public K Key;
public V Value;
}
private readonly List<KeyValuePair> fList = new List<KeyValuePair>();
public void Add(K key, V value)
{
fList.Add(new KeyValuePair { Key = key, Value = value });
}
public List<V> this[K key]
{
get { return (from pair in fList where pair.Key.Equals(key) select pair.Value).ToList(); }
}
public List<K> Keys
{
get { return fList.Select(pair => pair.Key).ToList(); }
}
public List<V> Values
{
get { return fList.Select(pair => pair.Value).ToList(); }
}
}