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?
The expected API is to add via the two argument
Add(key,value)method (or thethis[key]indexer); as such, it uses explicit interface implementation to provide theAdd(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:
which uses the
Add(key,value)method.