List in CSharpList is a generic collection that implements a dynamic array. Its conceptually similar to the non-generic
ArrayList. Sometimes we need an array to store large number of generic elements but at runtime we don’t know how large or how small it can be. To handle this situation C# has introduced List that expands and shrinks at runtime.
List<T> has the following constructors
public List( )
This constructor builds an empty List with a default initial capacity.
public List(IEnumerable<T> c)
It builds a List that is initialized with the elements of the collection specified by c and with an initial capacity equal to the number of elements.
public List(int capacity)
Its an array list that has the specified initial capacity. The capacity is the size of the array that is used to store the elements. The capacity grows automatically as elements are added to a List<T>. Each time the list must be enlarged, its capacity is increased.
List<T> has the following frequently used methods.
AddRange()
public void AddRange(IEnumerable<T> c)
Adds the elements in c to the end of the invoking list.
BinarySearch()
public virtual int BinarySearch(T v)
Searches the invoking collection for the value passed
in v. The index of the matching element is returned. If
the value is not found, a negative value is returned. The
invoking list must be sorted
IndexOf()
public int IndexOf(T v)
Returns the index of the first occurrence of v in the
invoking collection. Returns –1 if v is not found.
LastIndexOf()
public int LastIndexOf(T v) Returns the index of the last occurrence of v in the
invoking collection. Returns –1 if v is not found.
Reverse( )
public void Reverse( )
Reverses the contents of the invoking collection.
Sort( )
public void Sort( )
Sorts the collection into ascending order.
Sort(IComparer<T> comp)
public void Sort(IComparer<T> comp)
Sorts the collection using the specified comparison object. If comp is null, the default comparer for each
object is used.
Sort(Comparison<T> comp)
public void Sort(Comparison<T> comp)
Sorts the collection using the specified comparison delegate
ToArray( )
public T[ ] ToArray( )
Returns an array that contains copies of the elements
of the invoking object.
TrimExcess( )
public void TrimExcess( )
Reduces the capacity of the invoking list so that it is no more than 10 percent greater than the number of
elements that it currently holds.
List<T> defines a very useful properties Capacity,
public int Capacity { get; set; }
Capacity gets or sets the capacity of the list. The capacity is the number of elements that can be held before the list must be enlarged. Because a list grows automatically, it is not
necessary to set the capacity manually. However, for efficiency reasons, you might want to set the capacity when you know in advance how many elements the list will contain. This prevents the overhead associated with the allocation of more memory.
Here is a program the demonstrates List<T>.
// Demonstrate List<T>.
We need to add following namespace to access List<T> class.
using System;
using System.Collections.Generic;
// Create a list.
List<char> lst = new List<char>();
Response.Write("Initial number of elements: " +
lst.Count);
Response.Write();
Response.Write("Adding 6 elements");
// Add elements to the list.
lst.Add('C');
lst.Add('A');
lst.Add('E');
lst.Add('B');
lst.Add('D');
lst.Add('F');
Response.Write("Number of elements: " +
lst.Count);
// Display the list using array indexing.
Response.Write("Current contents: ");
for(int i=0; i < lst.Count; i++)
Response.Write(lst[i] + " ");
Response.Write("\n");
Response.Write("Removing 2 elements");
// Remove elements from the list.
lst.Remove('F');
lst.Remove('A');
Response.Write("Number of elements: " +lst.Count);
// Use foreach loop to display the list.
Response.Write("Contents: ");
foreach(char c in lst)
Response.Write(c + " ");
Response.Write("\n");
Response.Write("Adding 20 more elements");
// Add enough elements to force lst to grow.
for(int i=0; i < 20; i++)
lst.Add((char)('a' + i));
Response.Write("Current capacity: " +lst.Capacity);
Response.Write("Number of elements after adding 20: " +lst.Count);
Response.Write("Contents: ");
foreach(char c in lst)
Response.Write(c + " ");
Response.Write("\n");
// Change contents using array indexing.
Response.Write("Change first three elements");
lst[0] = 'X';
lst[1] = 'Y';
lst[2] = 'Z';
Response.Write("Contents: ");
foreach(char c in lst)
Response.Write(c + " ");
Response.Write();
// Because of generic type-safety,
// the following line is illegal.
// lst.Add(99); // Error, not a char
Output
Initial number of elements: 0
Adding 6 elements
Number of elements: 6
Current contents: C A E B D F
Removing 2 elements
Number of elements: 4
Contents: C E B D
Adding 20 more elements
Current capacity: 32
Number of elements after adding 20: 24
Contents: C E B D a b c d e f g h i j k l m n o p q r s t
Change first three elements
Contents: X Y Z D a b c d e f g h i j k l m n o p q r s t