IEnumerable Interface

I do not understand why IList implements IEnumerable , given that IList implements ICollection , which also implements IEnumerable .

+4
source share
3 answers

I assume that you want to know why it states that it implements ICollection , as well as IEnumerable , when the former implies the latter. I suspect the main reason is clarity: this means that people do not need to look back at ICollection to verify that this is already expanding IEnumerable .

There are other cases where you need to override the implementation of an interface if you want to reimplement an interface method that was previously explicitly implemented in the base class, but I don’t think the reason is this.

EDIT: I assume that the source code from which the documents are built contains a declaration that includes both interfaces. Another possible alternative is that all interfaces in the hierarchy are automatically pulled in by the doc generator. In this case, the question becomes β€œwhy does the doc generator do this” - and the answer almost certainly remains β€œclear”.

+11
source

IList implements only IEnumerable by associaton; those. it implements IEnumerable exactly because it inherits ICollection , which is equal to IEnumerable . You will get the same with type hierarchies (although only one inheritance:

 class Enumerable {} class Collection : Enumerable {} class List : Collection {} 

therefore List is Enumerable ; In the same way, IList IEnumerable .

For example, if I write:

 interface IA {} interface IB : IA { } interface IC : IB { } 

And look at the metadata, then it turns out that IC : IA, IB - but this is only indirect; here is IL:

 .class private interface abstract auto ansi IA { } .class private interface abstract auto ansi IB implements IA { } .class private interface abstract auto ansi IC implements IB, IA { } 
+8
source

If you were to open the code in ReSharper, this would mean that declaring the IEnumerable interface on an IList not required, as it is already specified through ICollection .

+1
source

All Articles