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

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

我正在查看为 IDictionary<TKey, TValue> 接口生成的元数据,我注意到它实现了

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

这不是多余的吗?查看 ICollection<T> 的元数据表明它已经实现了 IEnumerable<T>, IEnumerable

为什么 IEnumerable<T> 实现了 IEnumerable,而 ICollection<T> 没有实现 ICollection

元数据显示了所有已实现的接口,尤其是那些通过继承获得的接口。如果查看 IDictionary<TKey, TValue>reference source,您会发现实际实现仅实现 ICollection<KeyValuePair<TKey, TValue>>:

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

阅读this。它解释了您想知道的关于第一个问题的所有信息。

我引用最相关的部分:

Why do tools like Reflector or the object browser show the whole list?

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.