C# | ArrayList Class - GeeksforGeeks

03 Apr, 2019
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.
Example:
// C# code to create an ArrayListusing 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);    }}
Output:
Count : 2
Capacity : 4

Example:
// C# program to illustrate the // ArrayList Class Propertiesusing 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);     } } 
Output:
False
False

Example 1:
// C# code to check if an element is// contained in ArrayList or notusing 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");    }}
Output:
Yes, exists at index 4

Example 2:
// C# code to remove a range of// elements from the ArrayListusing 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);        }    }}
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:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist?view=netframework-4.7.2
CSharp-Collections-ArrayList
CSharp-Collections-Namespace
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between Abstract Class and Interface in C#
C# | IsNullOrEmpty() Method
C# | How to check whether a List contains a specified element
String.Split() Method in C# with Examples
C# | Arrays of Strings
C# | String.IndexOf( ) Method | Set - 1
Extension Method in C#
C# | Delegates
Difference between Ref and Out keywords in C#
C# | Replace() Method