How to use ArrayList in C#?
In this article, you will learn how to use ArrayList in C#. ArrayList is a collection of variable-length arrays. I will show you how to initialize, add, delete, sort, and remove duplicates.
Learn more:
ArrayList initialization
This section shows how to initialize an ArrayList in C#.
You can initialize an ArrayList object as follows:
ArrayList list = new ArrayList();
Or:
ArrayList list = new ArrayList() { "Apple", "Orange", "Grape" };
Adding elements
This section shows how to add elements to an ArrayList in C#.
To add a new element to an ArrayList, use the Add() method.
ArrayList list = new ArrayList(); list.Add("Apple"); list.Add("Grape");
Sample code:
using System; using System.Collections; namespace CSharpExample { class Program { static void Main(string[] args) { //Initialize ArrayList list = new ArrayList() { "Apple", "Orange", "Grape", "Pineapple" }; Print(list); //Add elements list.Add("Orange"); list.Add("Apple"); Console.WriteLine("*********************"); Console.WriteLine("After adding elements:"); Print(list); } static void Print(ArrayList list) { foreach (object element in list) { Console.WriteLine("Element: {0}", element); } } } }
Output:
Element: Apple
Element: Orange
Element: Grape
Element: Pineapple
*********************
After adding elements:
Element: Apple
Element: Orange
Element: Grape
Element: Pineapple
Element: Orange
Element: Apple
Deleting elements
This section shows how to delete and add ArrayList in C#.
To delete elements from an ArrayList, use the Remove() or RemoveAt() method.
//Initialize ArrayList list = new ArrayList() { "Apple", "Orange", "Grape" }; //Delete using the Remove() method list.Remove("Orange"); //Delete using the RemoveAt() method list.RemoveAt(0);
Sample code:
using System; using System.Collections; namespace CSharpExample { class Program { static void Main(string[] args) { //Initialize ArrayList list = new ArrayList() { "Apple", "Orange", "Grape", "Pineapple", "Melon", "Watermelon" }; Print(list); list.RemoveAt(0); list.Remove("Pineapple"); list.Remove("Orange"); Console.WriteLine("*********************"); Console.WriteLine("After removing elements:"); Print(list); } static void Print(ArrayList list) { foreach (object element in list) { Console.WriteLine("Element: {0}", element); } } } }
Output:
Element: Apple
Element: Orange
Element: Grape
Element: Pineapple
Element: Melon
Element: Watermelon
*********************
After removing elements:
Element: Grape
Element: Melon
Element: Watermelon
Sorting elements
This section shows how to sort elements of an ArrayList in C#.
To sort elements of an ArrayList in ascending order, use the Sort() method.
using System; using System.Collections; namespace CSharpExample { class Program { static void Main(string[] args) { //Initialize ArrayList list = new ArrayList() { "Apple", "Orange", "Grape", "Melon" }; Print(list); //Sort list.Sort(); Console.WriteLine("***********************"); Console.WriteLine("Sorting in ascending order:"); Print(list); } static void Print(ArrayList list) { foreach (object element in list) { Console.WriteLine("Element: {0}", element); } } } }
Output:
Element: Apple
Element: Orange
Element: Grape
Element: Melon
***********************
Sorting in ascending order:
Element: Apple
Element: Grape
Element: Melon
Element: Orange
To order the elements of an ArrayList in descending order, use the Sort() and Reverse() methods.
using System; using System.Collections; namespace CSharpExample { class Program { static void Main(string[] args) { //Initialize ArrayList list = new ArrayList() { "Apple", "Orange", "Pineapple", "Grape", "Melon" }; Print(list); //Sort list.Sort(); Console.WriteLine("***********************"); Console.WriteLine("Sorting in descending order:"); //Reverse list.Reverse(); Print(list); } static void Print(ArrayList list) { foreach (object element in list) { Console.WriteLine("Element: {0}", element); } } } }
Output:
lement: Apple
Element: Orange
Element: Pineapple
Element: Grape
Element: Melon
***********************
Sorting in descending order:
Element: Pineapple
Element: Orange
Element: Melon
Element: Grape
Element: Apple
Removing duplicate elements
Learn how to remove duplicate elements in an ArrayList in C#.
To remove duplicates from an ArrayList, you can use Linq's Distinct() as shown in the sample code below.
using System; using System.Collections; using System.Linq; namespace CSharpExample { class Program { static void Main(string[] args) { //Initialize ArrayList list = new ArrayList() { "Apple", "Orange", "Pineapple", "Grape", "Melon", "Orange", "Apple", "Grape" }; Print(list); Console.WriteLine("***********************"); Console.WriteLine("Removing duplicates:"); IEnumerable<object> list2 = list.Cast<object>().Distinct(); Print2(list2); } static void Print(ArrayList list) { foreach (object element in list) { Console.WriteLine("Element: {0}", element); } } static void Print2(IEnumerable<object> list) { foreach (object element in list) { Console.WriteLine("Element: {0}", element); } } } }
Output:
Element: Apple
Element: Orange
Element: Pineapple
Element: Grape
Element: Melon
Element: Orange
Element: Apple
Element: Grape
***********************
Removing duplicates:
Element: Apple
Element: Grape
Element: Melon
Element: Orange
Element: Pineapple
Summary
I hope you enjoyed this explanation of how to use ArrayList in C#, which is a collection of variable-length arrays. I have shown you how to initialize, add, delete, sort ArrayList.