Why does List <T> implement so many interfaces?
List<T> comes from the following interfaces:
public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable I'm just wondering why he needs all of these interfaces ( in a class declaration )?
IList itself already comes from ICollection<T> , IEnumerable<T> and IEnumerable .
So why not enough?
public class List<T> : IList<T>, IList Hope you can solve my confusion.
Indeed, List<T> just been implemented as
public class List<T> : IList<T>, IList Is this a reflector or such a decompiler showing you all the interfaces in inheritance.
try it
public class List2<T> : IList<T> I just compiled this and looked in a reflector that shows it as
public class List2<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable If you look into the .NET source code, you will see that it does not contain unnecessary references to all 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 simply lists all the interfaces.
You can get the .NET source code here or do a quick search here (it seems stuck on .NET4).
IMO it is impossible to determine how the actual implementation of List<T> was actually implemented. Perhaps it was:
public class List<T> : IList<T>, ICollection<T>, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable or maybe it was a simplified version ... although I think your example skips the ReadOnly interfaces, I still get the point.
public class List<T> : IList<T>, IList However, from the point of view of a simple understanding for any future developer (who may not be inclined to scan the entire inheritance chain), I think that the first form probably has its advantages.