Namespace: System.Collections.Generic
Assembly: mscorlib (in mscorlib.dll)
[SerializableAttribute]
public class List
The List class is the generic equivalent of the ArrayList class. It implements the IList generic interface using the array whose size is dynamically increased as required.
The List class uses both an equality comparer and an ordering comparer.
Methods such as Contains, IndexOf, LastIndexOf, and Remove use an equality comparer for the list elements. The default equality comparer for type T is determined as follows. If type T implements the IEquatable generic interface, then the equality comparer is the Equals method of that interface; otherwise, the default equality comparer is Object.Equals(Object).
Methods such as BinarySearch and Sort use an ordering comparer for the list elements. The default comparer for type T is determined as follows. If type T implements the IComparable generic interface, then the default comparer is the CompareTo method of that interface; otherwise, if type T implements the nongeneric IComparable interface, then the default comparer is the CompareTo method of that interface. If type T implements neither interface, then there is no default comparer, and a comparer or comparison delegate must be provided explicitly.
The List is not guaranteed to be sorted. You must sort the List before performing operations (such as BinarySearch) that require the List to be sorted.
Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.
List accepts a null reference (Nothing in Visual Basic) as a valid value for reference types and allows duplicate elements
Performance Considerations
In deciding whether to use the List or ArrayList class, both of which have similar functionality, remember that the List class performs better in most cases and is type safe. If a reference type is used for type T of the List class, the behavior of the two classes is identical. However, if a value type is used for type T, you need to consider implementation and boxing issues.
If a value type is used for type T, the compiler generates an implementation of the List class specifically for that value type. That means a list element of a List object does not have to be boxed before the element can be used, and after about 500 list elements are created the memory saved not boxing list elements is greater than the memory used to generate the class implementation.
Example
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
List
Console.WriteLine("\nCapacity: {0}", names.Capacity);
names.Add("Vishal");
names.Add("Joe");
names.Add("Prashant");
names.Add("Deepak");
Console.WriteLine();
foreach(string name in names)
{
Console.WriteLine(name);
}
Console.WriteLine("\nCapacity: {0}", names.Capacity);
Console.WriteLine("Count: {0}", names.Count);
Console.WriteLine("\nContains(\"Vishal\"): {0}",
names.Contains("Vishal"));
Console.WriteLine("\nInsert(2, \"Shobhit\")");
names.Insert(2, "Shobhit");
Console.WriteLine();
foreach(string name in names)
{
Console.WriteLine(name);
}
Console.WriteLine("\nnames[3]: {0}", names[3]);
Console.WriteLine("\nRemove(\"Deepak\")");
names.Remove("Deepak");
Console.WriteLine();
foreach(string name in names)
{
Console.WriteLine(name);
}
names.Clear();
Console.WriteLine("\nClear()");
Console.WriteLine("Capacity: {0}", names.Capacity);
Console.WriteLine("Count: {0}", names.Count);
}
}