Question

I don't understand why IList implements IEnumerable taking in consideration that IList implements ICollection that implements IEnumerable also.

Was it helpful?

Solution

I assume you want to know why it declares that it implements ICollection as well as IEnumerable, when the first implies the second. I suspect the main reason is clarity: it means people don't need to look back to ICollection to check that that already extends IEnumerable.

There are other times when you need to redeclare an interface implementation, if you want to re-implement an interface method which was previously explicitly implemented in a base class - but I don't think that's the reason in this case.

EDIT: I've been assuming that the source code that the docs are built from has the declaration including both interfaces. Another possible alternative is that all interfaces in the hierarchy are automatically pulled in by the doc generator. In which case the question becomes "why does the doc generator do that" - and the answer is almost certainly still "clarity".

OTHER TIPS

IList only implements IEnumerable by associaton; i.e. it implements IEnumerable precisely because it inherits ICollection which is IEnumerable. You'd get the same with type hierarchies (although only single inheritance:

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

so List is an Enumerable; in the same way, IList is IEnumerable.

For example, if I write:

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

And look at the metadata, then it appears that IC : IA, IB - but this is only indirect; here's the 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
{
}

If you were to open the code in ReSharper, it would indicate that the IEnumerable interface declaration on IList wasn't required, as it is already specified via ICollection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top