C#:Dictionary<K,V> 如何在没有 Add(KeyValuePair<K,V>) 的情况下实现 ICollection<KeyValuePair<K,V>>?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/268064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:35:15  来源:igfitidea点击:

C#: How can Dictionary<K,V> implement ICollection<KeyValuePair<K,V>> without having Add(KeyValuePair<K,V>)?

c#.netdictionaryinterface

提问by David Schmitt

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.

看一下System.Collections.Generic.Dictionary<TKey, TValue>,它清楚地实现了ICollection<KeyValuePair<TKey, TValue>>,但没有所需的“ void Add(KeyValuePair<TKey, TValue> item)”功能。

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

尝试Dictionary像这样初始化 a 时也可以看到这一点:

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

方法“Add”没有重载需要“1”个参数

Why is that so?

为什么呢?

采纳答案by Marc Gravell

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.

预期的 API 是通过两个参数Add(key,value)方法(或this[key]索引器)添加;因此,它使用显式接口实现来提供Add(KeyValuePair<,>)方法。

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).

如果您改用IDictionary<string, int>接口,您将可以访问缺少的方法(因为您无法在接口上隐藏任何内容)。

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.

使用该Add(key,value)方法。

回答by Pop Catalin

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();

回答by GeekyMonkey

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

它不ICollection<KeyValuePair<K,V>>直接实现。它实现IDictionary<K,V>.

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

IDictionary<K,V>源自ICollection<KeyValuePair<K,V>>.