ArrayList Collection Type
An ArrayList is a sophisticated version of an array. The ArrayList class provides some features that are offered in most Collections classes but are not in the Array class. For example:
- The capacity of an Array is fixed, whereas the capacity of an ArrayList 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 provides 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 using the Synchronized method. Array leaves it up to the user to implement synchronization.
- ArrayList provides 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 does not. For example:
- You can set the lower bound of an Array, but the lower bound of an ArrayList is always zero.
- An Array can have multiple dimensions, while an ArrayList always has exactly one dimension.
- An Array of a specific type (other than Object) has better performance than an ArrayList because the elements of ArrayList are of type Object and, therefore, boxing and unboxing typically occur if storing or retrieving a value type.
Most situations that call for an array can use ArrayList instead. It is easier to use and, in general, has performance similar to an array of type Object.
Array is in the System namespace; ArrayList is in the System.Collections namespace.
See Also
Generic Collection Types | ArrayList | System.Collections Namespace | Array