开发者

Why doesn't Dictionary<TKey, TValue> not have a Add method that takes a KeyValuePair object?

开发者 https://www.devze.com 2023-03-29 02:12 出处:网络
According to the .NET API, the class Dic开发者_运维知识库tionary<TKey, TValue> is inherited from ICollection<T>, where T is KeyValuePair<TKey, TValue>. How does the Dictionary<TKe

According to the .NET API, the class Dic开发者_运维知识库tionary<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

  • How to hide some members of an interface
  • http://www.iridescence.no/post/HidingInterfaceMembers.aspx

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.


Because the Dictionary has an explicit implementation of ICollection.Add. You'd need to cast it to ICollection<KeyValuePair<TKey,TValue>> before you could use it.

You can see the implementation on MSDN

void ICollection<KeyValuePair<TKey, TValue>>.Add(
    KeyValuePair<TKey, TValue> keyValuePair
)


For reference, the Dictionary<> code for the explicitly implemented ICollection<>.Add() method is:

void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> keyValuePair)
{
    this.Add(keyValuePair.Key, keyValuePair.Value);
}

You should be fine to use it, since it's just doing what you would likely have done yourself.


If Dictionary directly exposed an Add(KeyValuePair<TKey,TValue>) overload, that would suggest that if one had a KeyValuePair<TKey,TValue> (e.g. received when one was enumerating another dictionary) it would be better to pass it to such a method than to read out the Key and Value properties and pass them separately. In fact, the reverse is true: passing the whole structure to an Add method which then had to decompose it and pass its members to a discrete-parameter overload would be less efficient than decomposing the structure into its members and passing them separately.

Note that if the Dictionary stored things internally in an array of a structure type which had KeyValuePair<TKey,TValue> as a field (not property), then code like:

for var item in Dict1
{
  Dict2.Add(item);
}

could be more efficient than

for var item in Dict1
{
  Dict2.Add(item.Key, Item.Value);
}

but since Dictionary will decompose any passed-in KeyValuePair, there's no reason to encourage the former style of code.

0

精彩评论

暂无评论...
验证码 换一张
取 消