实现了 ICollection 和 IEnumerable,c#,interface,implementation"/>

首页 > 解决方案 > 为什么要字典实现了 ICollection 和 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

标签: c#interfaceimplementation

解决方案


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

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

推荐阅读