C# | ArrayList Class

ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list.

Properties of ArrayList Class:

  • Elements can be added or removed from the Array List collection at any point in time.
  • The ArrayList is not guaranteed to be sorted.
  • The capacity of an ArrayList is the number of elements the ArrayList can hold.
  • Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.
  • It also allows duplicate elements.
  • Using multidimensional arrays as elements in an ArrayList collection is not supported.

Constructors

Constructor Description
ArrayList() Initializes a new instance of the ArrayList class that is empty and has the default initial capacity.
ArrayList(ICollection) Initializes a new instance of the ArrayList class that contains elements copied from the specified collection and that has the same initial capacity as the number of elements copied.
ArrayList(Int32) Initializes a new instance of the ArrayList class that is empty and has the specified initial capacity.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to create an ArrayList
using System;
using System.Collections;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating an ArrayList
        ArrayList myList = new ArrayList();
  
        // Adding elements to ArrayList
        myList.Add("Hello");
        myList.Add("World");
  
        Console.WriteLine("Count : " + myList.Count);
        Console.WriteLine("Capacity : " + myList.Capacity);
    }
}

chevron_right


Output:

Count : 2
Capacity : 4

Properties

Property Description
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.
IsSynchronized Gets a value indicating whether access to the ArrayList is synchronized (thread safe).
Item[Int32] Gets or sets the element at the specified index.
SyncRoot Gets an object that can be used to synchronize access to the ArrayList.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the 
// ArrayList Class Properties
using System; 
using System.Collections; 
using System.Collections.Generic; 
    
class GFG { 
    
    // Driver code 
    public static void Main() 
    
    
        // Creating an ArrayList 
        ArrayList myList = new ArrayList(); 
    
        // Adding elements to ArrayList 
        myList.Add("A"); 
        myList.Add("B"); 
        myList.Add("C"); 
        myList.Add("D"); 
        myList.Add("E"); 
        myList.Add("F"); 
    
        // -------- IsFixedSize Property --------
          
        // To check if the ArrayList has fixed size or not 
        Console.WriteLine(myList.IsFixedSize); 
          
        // -------- IsReadOnly Property --------
          
        // To check if the ArrayList is read-only or not 
        Console.WriteLine(myList.IsReadOnly); 
    

chevron_right


Output:

False
False

Methods

Method Description
Adapter(IList) Creates an ArrayList wrapper for a specific IList.
Add(Object) Adds an object to the end of the ArrayList.
AddRange(ICollection) Adds the elements of an ICollection to the end of the ArrayList.
BinarySearch(Int32, Int32, Object, IComparer) Searches a range of elements in the sorted ArrayList for an element using the specified comparer and returns the zero-based index of the element.
BinarySearch(Object) Searches the entire sorted ArrayList for an element using the default comparer and returns the zero-based index of the element.
BinarySearch(Object, IComparer) Searches the entire sorted ArrayList for an element using the specified comparer and returns the zero-based index of the element.
Clear() Removes all elements from the ArrayList.
Clone() Creates a shallow copy of the ArrayList.
Contains(Object) Determines whether an element is in the ArrayList.
CopyTo(Array) Copies the entire ArrayList to a compatible one-dimensional Array, starting at the beginning of the target array.
CopyTo(Array, Int32) Copies the entire ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array.
CopyTo(Int32, Array, Int32, Int32) Copies a range of elements from the ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array.
Equals(Object) Determines whether the specified object is equal to the current object.
FixedSize(ArrayList) Returns an ArrayList wrapper with a fixed size.
FixedSize(IList) Returns an IList wrapper with a fixed size.
GetEnumerator() Returns an enumerator for the entire ArrayList.
GetEnumerator(Int32, Int32) Returns an enumerator for a range of elements in the ArrayList.
GetHashCode() Serves as the default hash function.
GetRange(Int32, Int32) Returns an ArrayList which represents a subset of the elements in the source ArrayList.
GetType() Gets the Type of the current instance.
IndexOf(Object) Searches for the specified Object and returns the zero-based index of the first occurrence within the entire ArrayList.
IndexOf(Object, Int32) Searches for the specified Object and returns the zero-based index of the first occurrence within the range of elements in the ArrayList that extends from the specified index to the last element.
IndexOf(Object, Int32, Int32) Searches for the specified Object and returns the zero-based index of the first occurrence within the range of elements in the ArrayList that starts at the specified index and contains the specified number of elements.
Insert(Int32, Object) Inserts an element into the ArrayList at the specified index.
InsertRange(Int32, ICollection) Inserts the elements of a collection into the ArrayList at the specified index.
LastIndexOf(Object) Searches for the specified Object and returns the zero-based index of the last occurrence within the entire ArrayList.
LastIndexOf(Object, Int32) Searches for the specified Object and returns the zero-based index of the last occurrence within the range of elements in the ArrayList that extends from the first element to the specified index.
LastIndexOf(Object, Int32, Int32) Searches for the specified Object and returns the zero-based index of the last occurrence within the range of elements in the ArrayList that contains the specified number of elements and ends at the specified index.
MemberwiseClone() Creates a shallow copy of the current Object.
ReadOnly(ArrayList) Returns a read-only ArrayList wrapper.
ReadOnly(IList) Returns a read-only IList wrapper.
Remove(Object) Removes the first occurrence of a specific object from the ArrayList.
RemoveAt(Int32) Removes the element at the specified index of the ArrayList.
RemoveRange(Int32, Int32) Removes a range of elements from the ArrayList.
Repeat(Object, Int32) Returns an ArrayList whose elements are copies of the specified value.
Reverse() Reverses the order of the elements in the entire ArrayList.
Reverse(Int32, Int32) Reverses the order of the elements in the specified range.
SetRange(Int32, ICollection) Copies the elements of a collection over a range of elements in the ArrayList.
Sort() Sorts the elements in the entire ArrayList.
Sort(IComparer) Sorts the elements in the entire ArrayList using the specified comparer.
Sort(Int32, Int32, IComparer) Sorts the elements in a range of elements in ArrayList using the specified comparer.
Synchronized(ArrayList) Returns an ArrayList wrapper that is synchronized (thread safe).
Synchronized(IList) Returns an IList wrapper that is synchronized (thread safe).
ToArray() Copies the elements of the ArrayList to a new Object array.
ToArray(Type) Copies the elements of the ArrayList to a new array of the specified element type.
ToString() Returns a string that represents the current object.
TrimToSize() Sets the capacity to the actual number of elements in the ArrayList.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to check if an element is
// contained in ArrayList or not
using System;
using System.Collections;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating an ArrayList
        ArrayList myList = new ArrayList();
  
        // Adding elements to ArrayList
        myList.Add("A");
        myList.Add("B");
        myList.Add("C");
        myList.Add("D");
        myList.Add("E");
        myList.Add("F");
        myList.Add("G");
        myList.Add("H");
  
        // To check if the ArrayList Contains element "E"
        // If yes, then display it's index, else
        // display the message
        if (myList.Contains("E"))
            Console.WriteLine("Yes, exists at index " + myList.IndexOf("E"));
        else
            Console.WriteLine("No, doesn't exists");
    }
}

chevron_right


Output:

Yes, exists at index 4

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to remove a range of
// elements from the ArrayList
using System;
using System.Collections;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating an ArrayList
        ArrayList myList = new ArrayList(10);
  
        // Adding elements to ArrayList
        myList.Add(2);
        myList.Add(4);
        myList.Add(6);
        myList.Add(8);
        myList.Add(10);
        myList.Add(12);
        myList.Add(14);
        myList.Add(16);
        myList.Add(18);
        myList.Add(20);
  
        // Displaying the elements in ArrayList
        Console.WriteLine("The initial ArrayList: ");
  
        foreach(int i in myList)
        {
            Console.WriteLine(i);
        }
  
        // removing 4 elements starting from index 0
        myList.RemoveRange(0, 4);
  
        // Displaying the modified ArrayList
        Console.WriteLine("The ArrayList after Removing elements: ");
  
        // Displaying the elements in ArrayList
        foreach(int i in myList)
        {
            Console.WriteLine(i);
        }
    }
}

chevron_right


Output:

The initial ArrayList: 
2
4
6
8
10
12
14
16
18
20
The ArrayList after Removing elements: 
10
12
14
16
18
20

Reference:



My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.