views:

433

answers:

3

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

+5  A: 

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
{
}
Marc Gravell
Whoever downvoted this just now, I do wish you'd have the courage to say why. I won't take it personally (unless, of course, it is purely a vendetta...)
Marc Gravell
+10  A: 

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".

Jon Skeet
+1 my gut is also telling me this is for clarity it is possible that this enables some reflection hacks that require a first level of inheritance
Sam Saffron
Since it is the interfaces (not the classes), re-implementation isn't really an issue.
Marc Gravell
I cant seem to find a way to get reflection to detect any difference, I think only the metadata interfaces know about this definition
Sam Saffron
A: 

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.

Drew Noakes