我正在查看为 IDictionary<TKey, TValue>
生成的元数据接口(interface),我注意到它实现了
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
?
最佳答案
元数据显示了所有已实现的接口(interface),尤其是那些通过继承获得的接口(interface)。如果你查看 reference source的 IDictionary<TKey, TValue>
, 你看到实际的实现只实现了 ICollection<KeyValuePair<TKey, TValue>>
:
public interface IDictionary<TKey, TValue> : ICollection<KeyValuePair<TKey, TValue>>
关于c# - 为什么 IDictionary<TKey, TValue> 同时实现 ICollection 和 IEnumerable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50170629/