What's the implementation of List?

2019-02-24 09:17发布

问题:

I read this code:

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

But I jumped to the definition(use VS2012) of List (in System.Collections.Generic), I found:

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

    ...

It's not Interface or Abstract, but it doesn't have function body(for any method in that class). I know ArrayList and LinkedList, but for List, I have no idea about its implementation.

My question:

  1. Where is the implementation of List?
  2. If List equals ArrayList or something, why .net will allow two class which equals function but different name? If List doesn't equal any other class in .NET, so why give it such an ambiguous name?

MSDN states:

The List class is the generic equivalent of the ArrayList class. It implements the IList generic interface by using an array whose size is dynamically increased as required.

So, I think it's a bad name...

回答1:

The implementation of List<T> can't be shown from Visual Studio because it doesn't have the source code present. It just shows the outline of the class (that is why Visual Studio puts [metadata] on top of the 'code file' when hitting F12).

The actual source can be found on referencesource.microsoft.com.

If List equals ArrayList or something, why .net will allow two class which equals function but different name? If List doesn't equal any other class in .NET, so why give it such an ambiguous name?

No, they are not the same. ArrayList is a non-generic list implementation, while List<T> is generic, and thus strongly typed.

Regarding the ambiguous name: I think Microsoft is right in their naming of List. ArrayList was a terrible name anyway. It emphasizes the implementation too much. You don't care there is an array behind it: to you it is just a List. Given that the name was available, this was a good option for a name.



回答2:

Where is the implementation of List?

What you're seeing is what VS allows you to see, it isn't actually the code but a brief summary of each methods documentation. If you want the code, the source is available here

Are List and ArrayList equal? If List doesn't equal any other class in .NET, so why give it such an ambiguous name?

List<T> doesn't equal ArrayList. A List<T> is strongly typed, while ArrayList uses object as it's internal collection, thus isn't strongly typed.

The former came to life when generics were introduced in .NET.

I don't think anything is ambiguous about List<T>. It is a list, which can contain any parameter as it's internal storage, such as a List<int>, List<string> or List<Foo>.