C# 为什么列表<;T>;实现这么多接口?

C# 为什么列表<;T>;实现这么多接口?,c#,derived-class,C#,Derived Class,列表源自以下接口: public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable 我希望你能解决我的困惑。确实List会像这样实现 public class List<T> : IList<T>, IList public class List2<T> : IList<T&

列表
源自以下接口:

public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
我希望你能解决我的困惑。

确实
List
会像这样实现

public class List<T> : IList<T>, IList
public class List2<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable
公共类列表:IList,IList
它是反射器或类似的反编译器,向您显示继承中的所有接口

试试这个

public class List2<T> : IList<T>
公共类列表2:IList
我刚刚编译了这个,并在reflector中查看,它显示如下

public class List<T> : IList<T>, IList
public class List2<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable
公共类列表2:IList、ICollection、IEnumerable、IEnumerable

IMO无法推断
List
的实际实现是如何编写的。可能是:

public class List<T> : IList<T>, ICollection<T>, IList, 
                       ICollection, IReadOnlyList<T>, 
                       IReadOnlyCollection<T>, IEnumerable<T>, 
                       IEnumerable
公共类列表:IList、ICollection、IList、,
ICollection,IReadOnlyList,
IReadOnlyCollection,IEnumerable,
数不清
或者它可能是一个简化版本。。。虽然我认为您的示例遗漏了只读接口,但我仍然理解这一点

public class List<T> : IList<T>, IList
公共类列表:IList,IList

然而,对于任何未来的开发人员来说(他们可能不倾向于一直扫描继承链),就容易理解而言,我认为第一种形式可能有它的好处。

如果您仔细查看实际的.NET源代码,您会发现它没有多余地提到所有接口:

// 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>
//实现一个可变大小的列表,该列表使用一个对象数组来存储
//元素。列表有一个容量,即分配的长度
//内部数组的。当元素添加到列表中时,容量
//通过重新分配
//内部数组。
// 
[DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView))]
[调试器显示(“计数={Count}”)]
[可序列化]
公共类列表:IList、System.Collections.IList、IReadOnlyList
反射器只是列出了所有的接口


您可以获取.NET的源代码,或者进行快速搜索(似乎停留在.NET4上)。

您从哪里看到它来自所有这些接口?可能正是您正在使用的工具扩展了IList实现者/派生工具/任何更好的描述符?很好的设计。
列表
的唯一来源是
系统。对象
,但它确实实现了很多接口。“因为它实现了很多功能”的论点完全没有抓住问题的关键。我不喜欢这样。我不想问任何人对此有什么意见。我想他是在问为什么会这样实现。我使用了Visual Studio 2012的类型定义和MSDN文档。@Andy我想MSDN文档会使层次结构扁平化,这样您就不必查找所有继承的接口。它可能只是为了供您参考和完整性而显示的。您不需要列出所有这些。您发布的第二个链接对代码造成了严重影响。在文档中搜索
,你就会明白我的意思。@spender Aye,我也看到了-这肯定是
列表的源代码,但是HTML格式已经去除了所有
。另外,如果你转到它的主页()并搜索
List
,它真的会发疯@斯彭德:好吧,我能说什么。。。在我的机器上工作™