简体   繁体   English

为什么IDictionary <TKey,TValue>实现了ICollection和IEnumerable

[英]Why IDictionary<TKey, TValue> implements both ICollection and IEnumerable

I was looking at the metadata generated for IDictionary<TKey, TValue> interface and I noticed it implements 我正在查看为IDictionary<TKey, TValue>界面生成的元数据,我注意到它实现了

public interface IDictionary<TKey, TValue> : ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable

Isn't this redundant? 这不是多余的吗? Looking at the metadata of ICollection<T> it shows that it already implements IEnumerable<T>, IEnumerable 查看ICollection<T>的元数据,它表明它已经实现了IEnumerable<T>, IEnumerable

And why does IEnumerable<T> implments IEnumerable , while ICollection<T> doesn't implement ICollection ? 为什么IEnumerable<T>暗示IEnumerable ,而ICollection<T>没有实现ICollection

The metadata show all implemented interfaces, especially those obtained through inheritance. 元数据显示所有已实现的接口,尤其是通过继承获得的接口。 If you look into the reference source of IDictionary<TKey, TValue> , you see that the actual implementation only implements ICollection<KeyValuePair<TKey, TValue>> : 如果你查看IDictionary<TKey, TValue>引用源 ,你会看到实际的实现只实现ICollection<KeyValuePair<TKey, TValue>>

public interface IDictionary<TKey, TValue> : ICollection<KeyValuePair<TKey, TValue>>

Read this . 这个 It explains everything you want to know about your first question. 它解释了您想要了解的关于第一个问题的所有信息。

I quote the most relevant part: 我引用最相关的部分:

Why do tools like Reflector or the object browser show the whole list? 为什么像Reflector或对象浏览器这样的工具会显示整个列表?

Those tools do not have the source code. 这些工具没有源代码。 They only have metadata to work from. 他们只有元数据可供使用。 Since putting in the full list is optional, the tool has no idea whether the original source code contains the full list or not. 由于放入完整列表是可选的,因此该工具不知道原始源代码是否包含完整列表。 It is better to err on the side of more information. 最好在更多信息方面犯错误。 Again, the tool is attempting to help you by showing you more information rather than hiding information you might need . 同样,该工具试图通过向您显示更多信息而不是隐藏您可能需要的 信息来帮助您。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么反射器不显示 IDictionary<TKey, TValue> 实现 IEnumerable<T> ? - Why does the reflector not show that IDictionary<TKey, TValue> implements IEnumerable<T>? 为什么IDictionary <TKey,TValue>扩展ICollection <KeyValuePair <TKey,TValue >>? - Why does IDictionary<TKey, TValue> extend ICollection<KeyValuePair<TKey, TValue>>? 如何转换IDictionary <TKey, List<TValue> &gt;到IDictionary <TKey, IEnumerable<TValue> &gt;? - How to convert IDictionary<TKey, List<TValue> > to IDictionary<TKey, IEnumerable<TValue> >? 为什么要向上转换IDictionary <TKey, TValue> 到IEnumerable <object> 失败? - Why does upcasting IDictionary<TKey, TValue> to IEnumerable<object> fail? 转换iDictionary <TKey,TValue> 进入IDictionary <TSomeProperty, ICollection<TValue> &gt; - Convert iDictionary<TKey,TValue> into IDictionary<TSomeProperty, ICollection<TValue>> 推荐的转换IGrouping的方法 <TKey, TValue> 到IDictionary <TKey, IEnumerable<TValue> &gt; - Recommended way to convert IGrouping<TKey, TValue> to IDictionary<TKey, IEnumerable<TValue>> 嵌套接口:演员表IDictionary <TKey, IList<TValue> &gt;到IDictionary <TKey, IEnumerable<TValue> &gt;? - Nested Interfaces: Cast IDictionary<TKey, IList<TValue>> to IDictionary<TKey, IEnumerable<TValue>>? 为什么不能/不能IDictionary <TKey,TValue>实现ILookup <TKey,TValue>? - Why doesn't/couldn't IDictionary<TKey,TValue> implement ILookup<TKey,TValue>? ObservableDictionary类 <TKey, TValue> :IDictionary <TKey, TValue> 作为DataContract - class ObservableDictionary<TKey, TValue> : IDictionary<TKey, TValue> as DataContract IEnumerable的 <T> 。去查查看 <TKey, TValue> - IEnumerable<T>.ToLookup<TKey, TValue>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM