According to the .NET API, the class Dictionary<TKey, TValue>
is inherited from ICollection<T>
, where T
is KeyValuePair<TKey, TValue>
. How does the Dictionary<TKey, TValue>
class hide some of the methods it inherits from ICollection<T>
?
For example:
ICollection<T>
has the method ICollection.Add(T item)
but when you use a Dictionary<TKey, TValue>
object it doesn't have that method. You can only use Dictionary<TKey, TValue>.Add(TKey key, TValue value)
. There is no Dictionary<TKey, TValue>.Add(KeyValuePair<TKey,TValue> kvp)
method.
Anyone know why? How are those methods hidden?
That is done by implementing the Interface explicitly and making this implementation private/protected... see
You could always cast the Dictionary
to ICollection
and then call Add
- though I wouldn't do this because I don't know whether it would work...
They're "hidden" use explicit interface implementation. So you can use:
ICollection<KeyValuePair<Foo, Bar>> collection = dictionary;
collection.Add(...);
According to the documentation that should work... although usually it would be simply to use an alternative approach.
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