[英]Collection<T>: why does it implement both IEnumerable and IEnumerable<T>?
I know exactly the same question appears here on StackOverflow , nevertheless it does not quite answer my query. 我知道完全相同的问题出现在StackOverflow上 ,但是它并不能完全回答我的查询。
If ICollection<T>
implements IEnumerable<T>
, which extends IEnumerable
, why did programmers from Microsoft add IEnumerable
as an interface that the ICollection<T>
implements? 如果
ICollection<T>
实现IEnumerable<T>
(扩展了IEnumerable
,为什么Microsoft的程序员将IEnumerable
添加为ICollection<T>
实现的接口?
Isn't it exactly (semantically and implementation wise) the same as simply writing ICollection<T> : IEnumerable<T>
? (在语义上和实现上)与编写
ICollection<T> : IEnumerable<T>
吗?
It says it implements IEnumerable
because it implements IEnumerable
. 它说它实现了
IEnumerable
因为它实现了IEnumerable
。
IEnumerable<T>
inherits IEnumerable
, but it can obviously provide no implementation. IEnumerable<T>
继承IEnumerable
,但显然不能提供任何实现。 Classes that implement IEnumerable<T>
must also implement IEnumerable
, whether or not they explicitly state that they do so. 实现
IEnumerable<T>
类也必须实现IEnumerable
,无论它们是否明确声明这样做。
class Foo : IEnumerable<T>
class Foo : IEnumerable<T>, IEnumerable
With either case, you implement the members of both interfaces. 无论哪种情况,都可以实现两个接口的成员。 The second class definition simply makes it obvious for those looking at the class and/or the documentation, which is a good thing.
第二类的定义对于查看类和/或文档的人员来说是显而易见的,这是一件好事。 An inexperienced or otherwise uninformed reader might not know that
IEnumerable<T>
brings IEnumerable
along with it. 没有经验的读者或其他不了解信息的读者可能不知道
IEnumerable<T>
将IEnumerable
与它一起带来。 They might might not know that a class that implements IEnumerable<T>
can be used where an IEnumerable
is expected. 他们可能不知道可以在期望
IEnumerable
地方使用实现IEnumerable<T>
的类。 This simply provides a bit more information to the reader, which can only be a good thing on average. 这只是向读者提供了更多信息,这平均而言只是一件好事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.