C# ArrayList with Examples

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:

  1. using System;
  2. using System.Collections;
  3. namespace CSharpExample
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. //Initialize
  10. ArrayList list = new ArrayList()
  11. {
  12. "Apple", "Orange", "Grape", "Pineapple"
  13. };
  14. //Print
  15. Print(list);
  16. //Add elements
  17. list.Add("Orange");
  18. list.Add("Apple");
  19. Console.WriteLine("*********************");
  20. Console.WriteLine("After adding elements:");
  21. Print(list);
  22. }
  23. static void Print(ArrayList list)
  24. {
  25. foreach (object element in list)
  26. {
  27. Console.WriteLine("Element: {0}", element);
  28. }
  29. }
  30. }
  31. }

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:

  1. using System;
  2. using System.Collections;
  3. namespace CSharpExample
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. //Initialize
  10. ArrayList list = new ArrayList()
  11. {
  12. "Apple", "Orange", "Grape", "Pineapple", "Melon", "Watermelon"
  13. };
  14. //Print
  15. Print(list);
  16. list.RemoveAt(0);
  17. list.Remove("Pineapple");
  18. list.Remove("Orange");
  19. Console.WriteLine("*********************");
  20. Console.WriteLine("After removing elements:");
  21. Print(list);
  22. }
  23. static void Print(ArrayList list)
  24. {
  25. foreach (object element in list)
  26. {
  27. Console.WriteLine("Element: {0}", element);
  28. }
  29. }
  30. }
  31. }

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.

  1. using System;
  2. using System.Collections;
  3. namespace CSharpExample
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. //Initialize
  10. ArrayList list = new ArrayList()
  11. {
  12. "Apple", "Orange", "Grape", "Melon"
  13. };
  14. //Print
  15. Print(list);
  16. //Sort
  17. list.Sort();
  18. Console.WriteLine("***********************");
  19. Console.WriteLine("Sorting in ascending order:");
  20. //Print
  21. Print(list);
  22. }
  23. static void Print(ArrayList list)
  24. {
  25. foreach (object element in list)
  26. {
  27. Console.WriteLine("Element: {0}", element);
  28. }
  29. }
  30. }
  31. }

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.

  1. using System;
  2. using System.Collections;
  3. namespace CSharpExample
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. //Initialize
  10. ArrayList list = new ArrayList()
  11. {
  12. "Apple", "Orange", "Pineapple", "Grape", "Melon"
  13. };
  14. //Print
  15. Print(list);
  16. //Sort
  17. list.Sort();
  18. Console.WriteLine("***********************");
  19. Console.WriteLine("Sorting in descending order:");
  20. //Reverse
  21. list.Reverse();
  22. //Print
  23. Print(list);
  24. }
  25. static void Print(ArrayList list)
  26. {
  27. foreach (object element in list)
  28. {
  29. Console.WriteLine("Element: {0}", element);
  30. }
  31. }
  32. }
  33. }

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.

  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. namespace CSharpExample
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. //Initialize
  11. ArrayList list = new ArrayList()
  12. {
  13. "Apple", "Orange", "Pineapple", "Grape",
  14. "Melon", "Orange", "Apple", "Grape"
  15. };
  16. //Print
  17. Print(list);
  18. Console.WriteLine("***********************");
  19. Console.WriteLine("Removing duplicates:");
  20. IEnumerable<object> list2 = list.Cast<object>().Distinct();
  21. //Print
  22. Print2(list2);
  23. }
  24. static void Print(ArrayList list)
  25. {
  26. foreach (object element in list)
  27. {
  28. Console.WriteLine("Element: {0}", element);
  29. }
  30. }
  31. static void Print2(IEnumerable<object> list)
  32. {
  33. foreach (object element in list)
  34. {
  35. Console.WriteLine("Element: {0}", element);
  36. }
  37. }
  38. }
  39. }

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.


See also:
C# List Contains with Examples
C# typeof with Examples
C# TryParse (int, double, float) with Examples
C# Data Types
C# Variables