Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IEnumerable interface

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

like image 803
Cornel Avatar asked Nov 30 '22 12:11

Cornel


2 Answers

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

like image 185
Jon Skeet Avatar answered Dec 04 '22 12:12

Jon Skeet


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
{
}
like image 24
Marc Gravell Avatar answered Dec 04 '22 13:12

Marc Gravell