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:
- Where is the implementation of
List
? - If
List
equalsArrayList
or something, why .net will allow two class which equals function but different name? IfList
doesn't equal any other class in .NET, so why give it such an ambiguous name?
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...
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.
No, they are not the same.
ArrayList
is a non-generic list implementation, whileList<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 aList
. Given that the name was available, this was a good option for a name.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
List<T>
doesn't equalArrayList
. AList<T>
is strongly typed, whileArrayList
usesobject
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 aList<int>
,List<string>
orList<Foo>
.