You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何ISet<T>需重复实现ICollection<T>已继承的IEnumerable系列接口?

Why does ISet<T> explicitly inherit IEnumerable<T> and IEnumerable when ICollection<T> already does?

Great question! This is a common point of confusion with C# interface inheritance, and there are a few key reasons behind this design choice:

1. Improved Readability & Documentation Clarity

When looking at the definition of ISet<T>, explicitly listing IEnumerable<T> and IEnumerable makes it immediately clear to developers that this interface supports enumeration—you don’t have to navigate up the inheritance chain to ICollection<T> to figure that out. It’s a form of self-documentation that makes the API easier to scan and understand at a glance.

2. API Design Consistency

Throughout the .NET Base Class Library (BCL), many collection interfaces follow this pattern of explicitly listing all inherited interfaces, even when they’re already covered by a direct parent interface. This creates a consistent look and feel across the API, so developers don’t have to guess whether a given interface includes certain capabilities based on indirect inheritance.

3. Historical Context & Backward Compatibility

Early versions of .NET had different interface design patterns, and as the framework evolved, maintaining explicit declarations helped preserve backward compatibility with existing code and documentation. Even though the compiler would treat a "simplified" ISet<T> : ICollection<T> definition identically, keeping the full inheritance list ensures that existing references, docs, and developer expectations remain intact.

Can ISet<T> just inherit ICollection<T> instead?

Absolutely! From a purely technical standpoint, removing the explicit references to IEnumerable<T> and IEnumerable would have no impact on the functionality of ISet<T>. Interface inheritance in C# is transitive, so ISet<T> would automatically inherit all members from ICollection<T>’s parent interfaces. The explicit declarations are purely a design choice for clarity and consistency, not a technical requirement.

内容的提问来源于stack exchange,提问作者Jacob Peterson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:02:58