[英]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.