What's the implementation of List?
Asked Answered
V

2

13

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

Venice answered 8/7, 2015 at 7:21 Comment(8)
referencesource.microsoft.com/#mscorlib/system/collections/…Groundsheet
Source code is here: reflector.webtropy.com/default.aspx/Net/Net/3@5@50727@3053/… - taken from #14914140Doralia
What makes you say that List is an ambiguous name?Premiership
@Premiership Maybe I'm used to Java.. In Java, List is abstract, we should declare a concrete implementation of List to use that.Venice
@Venice Java appears to call them ArrayList<E>. That's the same as List<T>.Resee
@Venice - But that doesn't make it ambiguous. Is there any other reason you think it is ambiguous?Premiership
@Premiership if it's the generic version of System.Collections.ArrayList, I think it may be better name it GenericArrayList. When we need a GenericLinkedList, what should we name it? AnotherList?Venice
@Venice - I see. You've got a point, but I think the issue is that someone was a bit dopey calling the non-generic list ArrayList. I think they figured it wasn't important calling a list by the name of the underlying data type.Premiership
E
24

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.

Enumerate answered 8/7, 2015 at 7:24 Comment(1)
There is a big difference in the time complexities of different operations in ArrayLists and LinkedLists. Knowing which one you're using can be important.Mefford
R
7

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

Resee answered 8/7, 2015 at 7:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.