[英]Why does ICollection<T> implement both IEnumerable<T> and IEnumerable
[英]Collection<T>: why does it implement both IEnumerable and IEnumerable<T>?
我知道完全相同的问题出现在StackOverflow上 ,但是它并不能完全回答我的查询。
如果ICollection<T>
实现IEnumerable<T>
(扩展了IEnumerable
,为什么Microsoft的程序员将IEnumerable
添加为ICollection<T>
实现的接口?
(在语义上和实现上)与编写ICollection<T> : IEnumerable<T>
吗?
它说它实现了IEnumerable
因为它实现了IEnumerable
。
IEnumerable<T>
继承IEnumerable
,但显然不能提供任何实现。 实现IEnumerable<T>
类也必须实现IEnumerable
,无论它们是否明确声明这样做。
class Foo : IEnumerable<T>
class Foo : IEnumerable<T>, IEnumerable
无论哪种情况,都可以实现两个接口的成员。 第二类的定义对于查看类和/或文档的人员来说是显而易见的,这是一件好事。 没有经验的读者或其他不了解信息的读者可能不知道IEnumerable<T>
将IEnumerable
与它一起带来。 他们可能不知道可以在期望IEnumerable
地方使用实现IEnumerable<T>
的类。 这只是向读者提供了更多信息,这平均而言只是一件好事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.