ArrayList

The ArrayList class supports dynamic arrays, which can grow or shrink as needed. In C#, standard arrays are of a fixed length, which cannot be changed during program execution. This means you must know in advance how many elements an array will hold. But sometimes you may not know until runtime precisely how large an array you will need.
 
To handle this situation, use ArrayList. An ArrayList is a variable-length array of object references that can dynamically increase or decrease in size. An ArrayList is created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array can be shrunk. ArrayList is currently in wide use in existing code. For this reason, it is examined in depth here. However, many of the same techniques that apply to ArrayList apply to the other collections as well, including the generic collections.
 
ArrayList supports several methods that operate on a range of elements within a collection. You can insert another collection into an ArrayList by calling InsertRange( ). You can remove a range by calling RemoveRange( ). You can overwrite a range within an ArrayList with the elements of another collection by calling SetRange( ). You can also sort or search a range rather than the entire collection.
 
By default, an ArrayList is not synchronized. To obtain a synchronized wrapper around a collection, call Synchronized( ).

Method Description
public virtual void AddRange(ICollection c) Adds the elements in c to the end of the invoking ArrayList.
public virtual int BinarySearch(object value) Searches the invoking collection for the value passed in value. 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.
public virtual int BinarySearch(object value,IComparer comparer) Searches the invoking collection for the value passed in value using the comparison object specified by comparer. 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.
public virtual int BinarySearch(int index,int count,object value,IComparer comparer) Searches the invoking collection for the value passed in value using the comparison object specified by comparer. The search begins at index and runs for count elements. 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.
public virtual void CopyTo(Array array) Copies the contents of the invoking collection to the array specified by array, which must be a one-dimensional array compatible with the type of the elements in the collection.
public virtual void CopyTo(Array array, int arrayIndex) Copies the contents of the invoking collection to the array specified by array, beginning at arrayIndex. The array must be a one-dimensional array compatible with the type of the elements in the collection.
public virtual void CopyTo(int index, Array array, int arrayIndex, int count) Copies a portion of the invoking collection, beginning at index and running for count elements, to the array specified by array, beginning at arrayIndex. array must be a onedimensional array compatible with the type of the elements in the collection.
public static ArrayList FixedSize(ArrayList list) Wraps list in a fixed-size ArrayList and returns the result.
public virtual ArrayList GetRange(int index, int count) Returns a portion of the invoking ArrayList. The range returned begins at index and runs for count elements. The returned object refers to the same elements as the invoking object.
public virtual int IndexOf(object value) Returns the index of the first occurrence of value in the invoking collection. Returns –1 if value is not found.
public virtual void InsertRange(int index, ICollection c) Inserts the elements of c into the invoking collection, starting at the index specified by index.
public virtual int LastIndexOf(object value) Returns the index of the last occurrence of value in the invoking collection. Returns –1 if value is not found.
public static ArrayList ReadOnly(ArrayList list) Wraps list in a read-only ArrayList and returns the result.
public virtual void RemoveRange(int index, int count) Removes count elements from the invoking collection, beginning at index.
public virtual void Reverse( ) Reverses the contents of the invoking collection.
public virtual void Reverse(int index, int count) Reverses count elements of the invoking collection, beginning at index.
public virtual void SetRange(int index, ICollection c) Replaces elements within the invoking collection, beginning at index, within those specified by c.
public virtual void Sort( ) Sorts the collection into ascending order.
public virtual void Sort(IComparer comparer) Sorts the collection using the specified comparison object. If comparer is null, the default comparison for each object is used.
public virtual void Sort(int index, int count, IComparer comparer) Sorts a portion of the collection using the specified comparison object. The sort begins at index and runs for count elements. If comparer is null, the default comparison for each object is used.
public static ArrayList Synchronized(ArrayList list) Returns a synchronized version of the invoking ArrayList.
public virtual object[ ] ToArray( ) Returns an array that contains copies of the elements of the invoking object.
public virtual Array ToArray(Type type) Returns an array that contains copies of the elements of the invoking object. The type of the elements in the array is specified by type.
public virtual void TrimToSize( ) Sets Capacity to Count.

Example

using System;
using System.IO;
using System.Collections;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList al = new ArrayList();

            Console.WriteLine("Adding some numbers:");
            al.Add(45);
            al.Add(78);
            al.Add(33);
            al.Add(56);
            al.Add(12);
            al.Add(23);
            al.Add(9);

            Console.WriteLine("Capacity: {0} ", al.Capacity);
            Console.WriteLine("Count: {0}", al.Count);

            Console.Write("Content: ");
            foreach (int i in al)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
            Console.Write("Sorted Content: ");
            al.Sort();
            foreach (int i in al)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
            Console.Read();
        }
    }
}