8

List<T> derives from the following interfaces:

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

I just wonder, why it needs all these interfaces (in the class declaration)?

IList itself already derives from ICollection<T>, IEnumerable<T> und IEnumerable.

So why is the following not enough?

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

I hope you can solve my confusion.

StingyJack
  • 19,041
  • 10
  • 63
  • 122
Andy
  • 3,997
  • 2
  • 19
  • 39
  • 2
    Where do you see that it derives from all of these interfaces? It may just be the tool you are using that is expanding the IList implementors/derivables/any better descriptor. – StingyJack Dec 06 '13 at 12:52
  • I like @AntP explanation; go and look at the definitions of all those interfaces which explains why it needs to implement all of them – wal Dec 06 '13 at 12:53
  • 1
    Because of the interface segregation principle? Just good design. – Cloud9999Strife Dec 06 '13 at 12:53
  • 8
    The only thing `List` **derives** from is `System.Object`, it does however **implement** a lot of interfaces. – Lukazoid Dec 06 '13 at 12:54
  • 10
    The "Because it implements lots of functionality" argument completely misses the point of the question. – spender Dec 06 '13 at 12:55
  • @Ant P - OP is asking why, if the interface IList already implements these other interfaces, that the class definition also lists all of these interfaces in the declaration. – StingyJack Dec 06 '13 at 12:57
  • 5
    I don't like this close votes. I don't think OP asking anyone opinion about that. I think he is asking _why_ is implemented like that. – Soner Gönül Dec 06 '13 at 12:58
  • 1
    How is that opionion based? OP clearly asks why there's a (seemingful) redundancy. – gwiazdorrr Dec 06 '13 at 13:01
  • Because each of these interfaces gives you some functionality: for example, Ienumerable gives you possibility to use keyword foreach. – VikciaR Dec 06 '13 at 13:11

3 Answers3

10

Indeed List<T> would have just implemented like this

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

It is the reflector or such decompiler shows you all the interfaces in inheritance.

Try this

public class List2<T> : IList<T>

I just compiled this and viewed in reflector, which shows like this

public class List2<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
2

If you take a peek into actual .NET source code you'll see that it does not redundantly mention all the interfaces:

// Implements a variable-size List that uses an array of objects to store the
// elements. A List has a capacity, which is the allocated length 
// of the internal array. As elements are added to a List, the capacity
// of the List is automatically increased as required by reallocating the
// internal array.
// 
[DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))]
[DebuggerDisplay("Count = {Count}")] 
[Serializable] 
public class List<T> : IList<T>, System.Collections.IList, IReadOnlyList<T>

The reflector just lists all the interfaces.

You can get the source code of .NET here, or do a quick search here (seems to stuck at .NET4).

gwiazdorrr
  • 6,181
  • 2
  • 27
  • 36
  • 2
    The second link you posted has done something really nasty to the code. Search the doc for `` and you'll see what I mean. – spender Dec 06 '13 at 13:12
  • @spender Aye, I saw that too - it's definitely the source code for `List` but the HTML formatting has stripped all the `<>`. Also, if you go to its home page (http://www.dotnetframework.org/Search.aspx) and search for `List` it *really* freaks out. :) – Matthew Watson Dec 06 '13 at 13:42
  • @spender: Well, what can I say... Works on my machine™ – gwiazdorrr Dec 06 '13 at 14:12
1

IMO it is impossible to deduce how the actual implementation of List<T> was actually written. It might have been:

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

or it might have been a simplified version... although I think your example misses out the ReadOnly interfaces, I still understand the point.

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

However, in terms of easy comprehension for any future developer (who might not be inclined to scan all the way up the inheritance chain), I think the first form probably has its benefits.

spender
  • 117,338
  • 33
  • 229
  • 351