–Array
The Array class is not part of the System.Collections namespaces. However, it is still a collection because it is based on the IList interface.
The rank of an Array object is the number of dimensions in the Array. An Array can have one or more ranks.
The lower bound of an Array is the index of its first element. An Array can have any lower bound. It has a lower bound of zero by default, but a different lower bound can be defined when creating an instance of the Array class using CreateInstance.
Unlike the classes in the System.Collections namespaces, Array has a fixed capacity. To increase the capacity, you must create a new Array object with the required capacity, copy the elements from the old Array object to the new one, and delete the old Array.
However, only the system and compilers can derive explicitly from the Array class. Users should use the array constructs provided by the language that they use.
–Arraylist
The ArrayList is not guaranteed to be sorted. You must sort the ArrayList prior to performing operations (such as BinarySearch) that require the ArrayList to be sorted.
The capacity of an ArrayList is the number of elements the ArrayList can hold. As elements are added to an ArrayList, the capacity is automatically increased as required through reallocation. The capacity can be decreased by calling TrimToSize or by setting the Capacity property explicitly.
Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.
The ArrayList collection accepts null as a valid value, allows duplicate elements.
Using multidimensional arrays as elements in an ArrayList collection is not supported.
—Array and arraylist
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, we 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.