ArrayList In C#

ArrayList

The ArrayList class is very useful if you are working with arrays and need to add, remove, index, or search for data in a collection. An ArrayList can also have values of different types stored in a sequential manner and the values can be added or removed as per the need.
 
Methods
 
Some of the common methods of ArrayList are as follows,
  1. Add (Add an item in an ArrayList)
    Syntax : ArrayList.add(object)

  2. Insert (Insert an item in a specified position in an ArrayList)
    Syntax : ArrayList.insert(index,object)

  3. Remove (Remove an item from ArrayList)
    Syntax :  ArrayList.Remove(object)

  4. RemoveAt (Remove an item from specified position)
    ArrayList.RemoveAt(index)

  5. Sort (Sort items in an ArrayList)
    ArrayList.Sort()
Example
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Collections;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7.   
  8. namespace ArrayListTest  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             ArrayList itemList = new ArrayList();  
  15.             itemList.Add("Item4");  
  16.             itemList.Add("Item5");  
  17.             itemList.Add("Item2");  
  18.             itemList.Add("Item1");  
  19.             itemList.Add("Item3");  
  20.             Console.WriteLine("\n\tShows Added Items in ArrayList\n");  
  21.             for (int i = 0; i <itemList.Count; i++)  
  22.             {  
  23.                 Console.WriteLine("\t"+itemList[i].ToString());  
  24.             }  
  25.             itemList.Insert(3, "Item6");    //Insert an item  
  26.             itemList.Sort();                //Sort items  
  27.             itemList.Remove("Item1");       //Remove an item  
  28.             itemList.RemoveAt(3);           //Remove item from specific position  
  29.               
  30.             Console.WriteLine("\n\tShows Final items in ArrayList\n");  
  31.             for (int i = 0; i < itemList.Count; i++)  
  32.             {  
  33.                 Console.WriteLine("\t"+itemList[i].ToString());  
  34.             }  
  35.             Console.Read();  
  36.         }  
  37.     }  
  38. }  
OUTPUT