C# - ArrayList Class

It represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. However, unlike array you can add and remove items from a list at a specified position using an index and the array resizes itself automatically. It also allows dynamic memory allocation, adding, searching and sorting items in the list.
The following table lists some of the commonly used properties of the ArrayList class −
Capacity
Gets or sets the number of elements that the ArrayList can contain.
Count
Gets the number of elements actually contained in the ArrayList.
IsFixedSize
Gets a value indicating whether the ArrayList has a fixed size.
IsReadOnly
Gets a value indicating whether the ArrayList is read-only.
Item
Gets or sets the element at the specified index.
The following table lists some of the commonly used methods of the ArrayList class −
public virtual int Add(object value);
Adds an object to the end of the ArrayList.
public virtual void AddRange(ICollection c);
Adds the elements of an ICollection to the end of the ArrayList.
public virtual void Clear();
Removes all elements from the ArrayList.
public virtual bool Contains(object item);
Determines whether an element is in the ArrayList.
public virtual ArrayList GetRange(int index, int count);
Returns an ArrayList which represents a subset of the elements in the source ArrayList.
public virtual int IndexOf(object);
Returns the zero-based index of the first occurrence of a value in the ArrayList or in a portion of it.
public virtual void Insert(int index, object value);
Inserts an element into the ArrayList at the specified index.
public virtual void InsertRange(int index, ICollection c);
Inserts the elements of a collection into the ArrayList at the specified index.
public virtual void Remove(object obj);
Removes the first occurrence of a specific object from the ArrayList.
public virtual void RemoveAt(int index);
Removes the element at the specified index of the ArrayList.
public virtual void RemoveRange(int index, int count);
Removes a range of elements from the ArrayList.
public virtual void Reverse();
Reverses the order of the elements in the ArrayList.
public virtual void SetRange(int index, ICollection c);
Copies the elements of a collection over a range of elements in the ArrayList.
public virtual void Sort();
Sorts the elements in the ArrayList.
public virtual void TrimToSize();
Sets the capacity to the actual number of elements in the ArrayList.
The following example demonstrates the concept −
using System;
using System.Collections;

namespace CollectionApplication {
   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.ReadKey();
      }
   }
}
When the above code is compiled and executed, it produces the following result −
Adding some numbers:
Capacity: 8
Count: 7
Content: 45 78 33 56 12 23 9
Content: 9 12 23 33 45 56 78    


    119 Lectures 
     23.5 hours 

 Raja Biswas

    37 Lectures 
     13 hours 

 Trevoir Williams

    16 Lectures 
     1 hours 

 Peter Jepson

    159 Lectures 
     21.5 hours 

 Ebenezer Ogbu

    193 Lectures 
     17 hours 

 Arnold Higuit

    24 Lectures 
     2.5 hours 

 Eric Frick
 Print
 Add Notes
 Bookmark this page