문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top