ArrayList and List Collection Types

A System.Collections.ArrayList or System.Collections.Generic.List<T> object is a sophisticated version of an array. The ArrayList class and the List<T> generic class provide some features that are offered in most System.Collections classes but that are not in the Array class. For example:

  • The capacity of an Array is fixed, whereas the capacity of an ArrayList or a List<T> is automatically expanded as required. If the value of the ArrayList.Capacity property is changed, the memory reallocation and copying of elements are automatically done.

  • ArrayList and List<T> provide methods that add, insert, or remove a range of elements. In Array, you can get or set the value of only one element at a time.

  • A synchronized version of ArrayList is easy to create by using the Synchronized method; however, this type of synchronization is relatively inefficient. The Array and List<T> classes leaves it up to the user to implement synchronization. The System.Collections.Concurrent namespace does not provide a concurrent list type, but it does provide a ConcurrentQueue<T> and ConcurrentStack<T> type.

  • ArrayList and List<T> provide methods that return read-only and fixed-size wrappers to the collection. Array does not.

On the other hand, Array offers some flexibility that ArrayList and List<T> do not. For example:

  • You can set the lower bound of an Array, but the lower bound of an ArrayList or a List<T> is always zero.

  • An Array can have multiple dimensions, but an ArrayList or a List<T> always has exactly one dimension. However, you can easily create a list of arrays or a list of lists.

  • An Array of a specific type (other than Object) provides better performance than an ArrayList. This is because the elements of ArrayList are of type Object; therefore, boxing and unboxing typically occur when you store or retrieve a value type. However, a List<T> can provide performance similar to an array of the same type if no reallocations are required; that is, if the initial capacity is a good approximation of the maximum size of the list.

Most situations that call for an array can use an ArrayList or a List<T> instead; they are easier to use and, in general, have performance similar to an array of the same type.

Array is in the System namespace; ArrayList is in the System.Collections namespace; List<T> is in the System.Collections.Generic namespace.

See Also

Reference

ArrayList

System.Collections

List<T>

System.Collections.Generic

Array

System.Collections.Concurrent

Other Resources

Commonly Used Collection Types