Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does (does it really?) List<T> implement all these interfaces, not just IList<T>?

Tags:

c#

.net

List declaration from MSDN:

public class List<T> : IList<T>, ICollection<T>,   IEnumerable<T>, IList, ICollection, IEnumerable 

Reflector gives similar picture. Does List really implement all of these (if yes why)? I have checked:

    interface I1 {}     interface I2 : I1 {}     interface I3 : I2 {}      class A : I3 {}     class B : I3, I2, I1 {}      static void Main(string[] args)     {         var a = new A();         var a1 = (I1)a;         var a2 = (I2)a;         var a3 = (I3)a;          var b = new B();         var b1 = (I1) b;         var b2 = (I2)b;         var b3 = (I3)b;     } 

it compiles.

[UPDATED]:

Guys, as i understand, all the replies stay that it:

class Program {      interface I1 {}     interface I2 : I1 {}     interface I3 : I2 {}      class A : I3 {}     class B : I3, I2, I1 {}      static void I1M(I1 i1) {}     static void I2M(I2 i2) {}     static void I3M(I3 i3) {}      static void Main(string[] args)     {         var a = new A();         I1M(a);         I2M(a);         I3M(a);          var b = new B();         I1M(b);         I2M(b);         I3M(b);          Console.ReadLine();     } } 

would give error, but it compiles and runs without any errors. Why?

like image 762
idm Avatar asked Jan 27 '11 14:01

idm


People also ask

Does List implement IList?

I believe List implements a version of IList because it is trivial to do, and may be useful, as you wouldn't need to wrap your list in another to pass it into another class. However it does do a type-check on any items you attempt to add and will throw if they don't match.

Does List implement IList C#?

List is the concrete class. This class implements IList, ICollection, IEnumerable.

What is IList t in c#?

The IList<T> generic interface is a descendant of the ICollection<T> generic interface and is the base interface of all generic lists.

Does List t implement IEnumerable?

List implements IEnumerable, but represents the entire collection in memory. LINQ expressions return an enumeration, and by default the expression executes when you iterate through it using a foreach, but you can force it to iterate sooner using . ToList() or . ToArray().


2 Answers

UPDATE: This question was the basis of my blog entry for Monday April 4th 2011. Thanks for the great question.

Let me break it down into many smaller questions.

Does List<T> really implement all those interfaces?

Yes.

Why?

Because when an interface (say, IList<T>) inherits from an interface (say IEnumerable<T>) then implementers of the more derived interface are required to also implement the less derived interface. That's what interface inheritance means; if you fulfill the contract of the more derived type then you are required to also fulfill the contract of the less derived type.

So a class is required to implement all the methods of all the interfaces in the transitive closure of its base interfaces?

Exactly.

Is a class that implements a more-derived interface also required to state in its base type list that it is implementing all of those less-derived interfaces?

No.

Is the class required to NOT state it?

No.

So it's optional whether the less-derived implemented interfaces are stated in the base type list?

Yes.

Always?

Almost always:

interface I1 {} interface I2 : I1 {} interface I3 : I2 {}  

It is optional whether I3 states that it inherits from I1.

class B : I3 {} 

Implementers of I3 are required to implement I2 and I1, but they are not required to state explicitly that they are doing so. It's optional.

class D : B {} 

Derived classes are not required to re-state that they implement an interface from their base class, but are permitted to do so. (This case is special; see below for more details.)

class C<T> where T : I3 {     public virtual void M<U>() where U : I3 {} } 

Type arguments corresponding to T and U are required to implement I2 and I1, but it is optional for the constraints on T or U to state that.

It is always optional to re-state any base interface in a partial class:

partial class E : I3 {} partial class E {} 

The second half of E is permitted to state that it implements I3 or I2 or I1, but not required to do so.

OK, I get it; it's optional. Why would anyone unnecessarily state a base interface?

Perhaps because they believe that doing so makes the code easier to understand and more self-documenting.

Or, perhaps the developer wrote the code as

interface I1 {} interface I2 {} interface I3 : I1, I2 {} 

and the realized, oh, wait a minute, I2 should inherit from I1. Why should making that edit then require the developer to go back and change the declaration of I3 to not contain explicit mention of I1? I see no reason to force developers to remove redundant information.

Aside from being easier to read and understand, is there any technical difference between stating an interface explicitly in the base type list and leaving it unstated but implied?

Usually no, but there can be a subtle difference in one case. Suppose you have a derived class D whose base class B has implemented some interfaces. D automatically implements those interfaces via B. If you re-state the interfaces in D's base class list then the C# compiler will do an interface re-implementation. The details are a bit subtle; if you are interested in how this works then I recommend a careful reading of section 13.4.6 of the C# 4 specification.

Does the List<T> source code actually state all those interfaces?

No. The actual source code says

public class List<T> : IList<T>, System.Collections.IList 

Why does MSDN have the full interface list but the real source code does not?

Because MSDN is documentation; it's supposed to give you as much information as you might want. It is much more clear for the documentation to be complete all in one place than to make you search through ten different pages to find out what the full interface set is.

Why does Reflector show the whole list?

Reflector only has metadata to work from. Since putting in the full list is optional, Reflector has no idea whether the original source code contains the full list or not. It is better to err on the side of more information. Again, Reflector is attempting to help you by showing you more information rather than hiding information you might need.

BONUS QUESTION: Why does IEnumerable<T> inherit from IEnumerable but IList<T> does not inherit from IList?

A sequence of integers can be treated as a sequence of objects, by boxing every integer as it comes out of the sequence. But a read-write list of integers cannot be treated as a read-write list of objects, because you can put a string into a read-write list of objects. An IList<T> is not required to fulfill the whole contract of IList, so it does not inherit from it.

like image 117
Eric Lippert Avatar answered Oct 14 '22 14:10

Eric Lippert


The non-generic interfaces are for backward compatibility. If you write code using generics and want to pass your list to some module written in .NET 1.0 (which doesn't have generics) you still want this to succeed. Hence IList, ICollection, IEnumerable.

like image 42
Ilya Kogan Avatar answered Oct 14 '22 14:10

Ilya Kogan