List的实现是什么?

13

我读了这段代码:

List<long> userIdList = new List<long>();

但是我跳转到 System.Collections.Generic 中的 List 的定义(使用 VS2012),我发现:

public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
{
    // Summary:
    //     Initializes a new instance of the System.Collections.Generic.List<T> class
    //     that is empty and has the default initial capacity.
    [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
    public List();
    //
    // Summary:
    //     Initializes a new instance of the System.Collections.Generic.List<T> class
    //     that contains elements copied from the specified collection and has sufficient
    //     capacity to accommodate the number of elements copied.
    //
    // Parameters:
    //   collection:
    //     The collection whose elements are copied to the new list.
    //
    // Exceptions:
    //   System.ArgumentNullException:
    //     collection is null.
    public List(IEnumerable<T> collection);
    //
    // Summary:
    //     Initializes a new instance of the System.Collections.Generic.List<T> class
    //     that is empty and has the specified initial capacity.
    //
    // Parameters:
    //   capacity:
    //     The number of elements that the new list can initially store.
    //
    // Exceptions:
    //   System.ArgumentOutOfRangeException:
    //     capacity is less than 0.
    [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
    public List(int capacity);

    // Summary:
    //     Gets or sets the total number of elements the internal data structure can
    //     hold without resizing.
    //
    // Returns:
    //     The number of elements that the System.Collections.Generic.List<T> can contain
    //     before resizing is required.
    //
    // Exceptions:
    //   System.ArgumentOutOfRangeException:
    //     System.Collections.Generic.List<T>.Capacity is set to a value that is less
    //     than System.Collections.Generic.List<T>.Count.
    //
    //   System.OutOfMemoryException:
    //     There is not enough memory available on the system.
    public int Capacity { get; set; }
    //
    // Summary:
    //     Gets the number of elements actually contained in the System.Collections.Generic.List<T>.
    //
    // Returns:
    //     The number of elements actually contained in the System.Collections.Generic.List<T>.
    public int Count { get; }

    // Summary:
    //     Gets or sets the element at the specified index.
    //
    // Parameters:
    //   index:
    //     The zero-based index of the element to get or set.
    //
    // Returns:
    //     The element at the specified index.
    //
    // Exceptions:
    //   System.ArgumentOutOfRangeException:
    //     index is less than 0.-or-index is equal to or greater than System.Collections.Generic.List<T>.Count.
    public T this[int index] { get; set; }

    // Summary:
    //     Adds an object to the end of the System.Collections.Generic.List<T>.
    //
    // Parameters:
    //   item:
    //     The object to be added to the end of the System.Collections.Generic.List<T>.
    //     The value can be null for reference types.
    public void Add(T item);

    ...

这个类既不是接口也不是抽象类,但它没有任何方法的函数体。我知道 ArrayListLinkedList,但对于 List,我不知道它的实现。

我的问题:

  1. List 的实现在哪里?
  2. 如果 List 等于 ArrayList 或其他什么东西,那么为什么 .NET 允许两个等效但名称不同的类?如果 List 在 .NET 中没有等效的类,那么为什么给它起一个如此模糊的名称?

MSDN 表示:

List 类是 ArrayList 类的泛型等效项。通过使用大小根据需要动态增加的数组,它使用 IList 泛型接口进行实现。

所以,我认为这是一个糟糕的名称...


5
这是一个指向 Microsoft .NET Framework 的参考资源,它展示了 C# 中 List<T> 泛型类的源代码实现。 - MarcinJuraszek
源代码在此处: http://reflector.webtropy.com/default.aspx/Net/Net/3@5@50727@3053/DEVDIV/depot/DevDiv/releases/whidbey/netfxsp/ndp/clr/src/BCL/System/Collections/Generic/List@cs/2/List@cs - 取自https://dev59.com/G27Xa4cB1Zd3GeqPq438 - Rob
你为什么说“List”是一个模糊的名称? - Enigmativity
@Enigmativity 或许我已经习惯了Java.. 在Java中,List是抽象的,我们需要声明一个具体的List实现才能使用它。 - Sayakiss
@Sayakiss - 但这并不意味着它是模棱两可的。你认为还有其他原因导致它模糊不清吗? - Enigmativity
显示剩余3条评论
2个回答

24
Visual Studio无法显示List<T>的实现,因为它没有源代码。它只显示类的大纲(这就是为什么在按F12时Visual Studio在'代码文件'顶部放置[metadata]的原因)。
实际源代码可以在referencesource.microsoft.com上找到。
如果List等于ArrayList或其他东西,为什么.NET会允许两个相等但名称不同的类?如果List不等于.NET中的任何其他类,则为什么要给它如此模糊的名称?
不,它们不同。ArrayList是非泛型列表实现,而List<T>是泛型并且具有强类型。
关于名称的歧义:我认为Microsoft在命名List方面做得很好。ArrayList本来就是个糟糕的名字。它过分强调了实现细节。你不在乎它后面有一个数组:对你来说它只是一个List。鉴于名称可用,这是一个很好的选项。

3
ArrayListLinkedList中不同操作的时间复杂度有很大差异。了解你正在使用的是哪个可能很重要。 - Elan-R

7

List的实现在哪里?

你所看到的只是VS允许你看到的内容,它不是实际的代码,而是每个方法文档的简要摘要。如果需要代码,源代码在这里可用

List和ArrayList是否相等?如果List在.NET中不等于任何其他类,为什么给它起一个这样含糊的名称?

List<T>不等于ArrayListList<T>是具有强类型的,而ArrayListobject作为其内部集合,因此没有强类型化。

前者在.NET引入泛型时出现。

我认为List<T>没有任何歧义。它是一个列表,可以包含任何参数作为其内部存储,如List<int>List<string>List<Foo>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接