为什么ArrayList的实现IList,ICollection的,IEnumerable的? [英] Why ArrayList implement IList, ICollection, IEnumerable?
问题描述
ArrayList的声明它实现IList,ICollection的,IEnumeralbe接口。
为什么不仅实现IList,因为IList中也从ICollection的衍生,是ICollection的自IEnumerable的。
ArrayList declare that it implements IList, ICollection, IEnumeralbe interfaces. Why not only implement IList, because IList is also derived from ICollection, ICollection is derived from IEnumerable.
这是什么样的申报的目的是什么? 。还有很多类似案例,在.NET基础类库
What's the purpose of this kind of declare? there are many cases like this in .net BCL.
推荐答案
通过下面的代码:
interface I1 { }
interface I2 : I1 { }
class Foo: I2 { }
如果您通过反射来看看富,你会发现
If you look at Foo through reflection you will find
class Foo: I2, I1 { }
这也是有效的编译和给出了相同的结果。
这样的差别是没有实际意义,并记录美孚你可能也与这两个接口编写。
Which is also valid to compile and gives the same result.
So the difference is moot, and in documenting Foo you might as well write it with both interfaces.
另请参阅在SO问题:为何集合类中C#(如ArrayList)从多个接口继承,如果这些接口中的一个从剩下的?
Also see the SO question: Why collections classes in C# (like ArrayList) inherit from multiple interfaces if one of these interfaces inherits from the remaining?
这篇关于为什么ArrayList的实现IList,ICollection的,IEnumerable的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!