11

Looking at System.Collections.Generic.Dictionary<TKey, TValue>, it clearly implements ICollection<KeyValuePair<TKey, TValue>>, but doesn't have the required "void Add(KeyValuePair<TKey, TValue> item)" function.

This can also be seen when trying to initialize a Dictionary like this:

private const Dictionary<string, int> PropertyIDs = new Dictionary<string, int>()
{
    new KeyValuePair<string,int>("muh", 2)
};

which fails with

No overload for method 'Add' takes '1' arguments

Why is that so?

SW4
  • 69,876
  • 20
  • 132
  • 137
David Schmitt
  • 58,259
  • 26
  • 121
  • 165

3 Answers3

19

The expected API is to add via the two argument Add(key,value) method (or the this[key] indexer); as such, it uses explicit interface implementation to provide the Add(KeyValuePair<,>) method.

If you use the IDictionary<string, int> interface instead, you will have access to the missing method (since you can't hide anything on an interface).

Also, with the collection initializer, note that you can use the alternative syntax:

Dictionary<string, int> PropertyIDs = new Dictionary<string, int> {
  {"abc",1}, {"def",2}, {"ghi",3}
}

which uses the Add(key,value) method.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
9

Some interface methods are implemented explicitly. If you use reflector you can see the explicitly implemented methods, which are:

void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> keyValuePair);
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> keyValuePair);
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int index);
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> keyValuePair);
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator();
void ICollection.CopyTo(Array array, int index);
void IDictionary.Add(object key, object value);
bool IDictionary.Contains(object key);
IDictionaryEnumerator IDictionary.GetEnumerator();
void IDictionary.Remove(object key);
IEnumerator IEnumerable.GetEnumerator();
Pop Catalin
  • 61,751
  • 23
  • 87
  • 115
0

It doesn't implement ICollection<KeyValuePair<K,V>> directly. It implements IDictionary<K,V>.

IDictionary<K,V> derives from ICollection<KeyValuePair<K,V>>.

Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
GeekyMonkey
  • 12,478
  • 6
  • 33
  • 39
  • That doesn't really answer the question - it must (to be valid) still *have* such an Add method - it juts happens to be an explicit implementation rather than part of the public class API. – Marc Gravell Nov 06 '08 at 09:56