C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# ArrayList Class: Dynamic Array

In this tutorial, we'll demonstrate how to use the ArrayList class in C#. The ArrayList class is a dynamic array, which means that its size can be changed during runtime. It is part of the System.Collections namespace.

  • Set up the environment

Create a new C# Console Application project in Visual Studio and add the following namespace:

using System.Collections;
  • Create an ArrayList

Create an instance of the ArrayList class:

ArrayList myArrayList = new ArrayList();
  • Add elements to the ArrayList

You can use the Add method to add elements to the ArrayList:

myArrayList.Add(10);
myArrayList.Add("Hello");
myArrayList.Add(3.14);

Note that the ArrayList can store different types of elements, unlike an array.

  • Access elements in the ArrayList

To access elements in the ArrayList, use the indexer property and provide the index:

int firstElement = (int)myArrayList[0];
string secondElement = (string)myArrayList[1];

Note that you need to cast the elements to their appropriate types since the ArrayList stores elements as objects.

  • Insert and remove elements in the ArrayList

Use the Insert method to insert an element at a specified index:

myArrayList.Insert(1, "World");

Use the Remove method to remove the first occurrence of an element:

myArrayList.Remove(3.14);

Use the RemoveAt method to remove an element at a specified index:

myArrayList.RemoveAt(2);
  • Loop through the ArrayList

Use a foreach loop to iterate through the elements of the ArrayList:

foreach (object item in myArrayList)
{
    Console.WriteLine($"Element: {item}");
}
  • ArrayList properties and methods

The ArrayList class has several useful properties and methods, such as Count, Contains(), and IndexOf(). For example:

int count = myArrayList.Count; // Returns the number of elements in the ArrayList
bool contains = myArrayList.Contains("Hello"); // Returns true if the ArrayList contains the specified element
int index = myArrayList.IndexOf("Hello"); // Returns the index of the first occurrence of the specified element in the ArrayList

In this tutorial, we've shown you how to use the ArrayList class in C#. The ArrayList class is a dynamic array that can store elements of different types and resize itself during runtime. However, it is worth noting that the ArrayList class is not type-safe and has some performance overhead due to boxing and unboxing of value types. In most cases, it is recommended to use the generic List<T> class from the System.Collections.Generic namespace, which provides type safety and better performance.

  1. C# ArrayList example:

    • Description: This example introduces the ArrayList class in C#, demonstrating its use as a dynamic array that can store elements of different types.
    • Code:
      // Create and initialize an ArrayList
      ArrayList arrayList = new ArrayList();
      arrayList.Add(1);
      arrayList.Add("Hello");
      arrayList.Add(3.14);
      
      // Access and print elements of the ArrayList
      foreach (var element in arrayList)
      {
          Console.WriteLine(element);
      }
      
  2. How to declare and initialize ArrayList in C#:

    • Description: This example guides you through the process of declaring and initializing an ArrayList in C#.
    • Code:
      // Declare and initialize an ArrayList
      ArrayList arrayList = new ArrayList();
      arrayList.Add(1);
      arrayList.Add(2);
      arrayList.Add(3);
      
  3. Accessing elements in an ArrayList in C#:

    • Description: Demonstrating how to access individual elements in an ArrayList using index notation.
    • Code:
      ArrayList arrayList = new ArrayList();
      arrayList.Add("Apple");
      arrayList.Add("Banana");
      
      // Access and print specific elements
      Console.WriteLine(arrayList[0]); // Output: Apple
      Console.WriteLine(arrayList[1]); // Output: Banana
      
  4. Iterating through an ArrayList in C#:

    • Description: Showcasing different methods for iterating through an ArrayList in C#, such as using foreach and traditional for loops.
    • Code:
      ArrayList arrayList = new ArrayList();
      arrayList.Add(1);
      arrayList.Add(2);
      arrayList.Add(3);
      
      // Using foreach loop
      foreach (var element in arrayList)
      {
          Console.WriteLine(element);
      }
      
      // Using for loop
      for (int i = 0; i < arrayList.Count; i++)
      {
          Console.WriteLine(arrayList[i]);
      }
      
  5. Adding and removing elements from ArrayList in C#:

    • Description: This example demonstrates how to add and remove elements dynamically from an ArrayList in C#.
    • Code:
      ArrayList arrayList = new ArrayList();
      arrayList.Add(1);
      arrayList.Add(2);
      arrayList.Add(3);
      
      // Add a new element
      arrayList.Add(4);
      
      // Remove an element
      arrayList.Remove(2);
      
  6. Sorting an ArrayList in C#:

    • Description: Illustrating how to sort an ArrayList in ascending order using the ArrayList.Sort method.
    • Code:
      ArrayList arrayList = new ArrayList();
      arrayList.Add(5);
      arrayList.Add(3);
      arrayList.Add(1);
      
      // Sort the ArrayList in ascending order
      arrayList.Sort();
      
      // Print the sorted ArrayList
      foreach (var element in arrayList)
      {
          Console.WriteLine(element);
      }
      
  7. Searching for an element in an ArrayList C#:

    • Description: Introducing methods for searching for an element in an ArrayList, such as using ArrayList.IndexOf.
    • Code:
      ArrayList arrayList = new ArrayList();
      arrayList.Add("Apple");
      arrayList.Add("Banana");
      arrayList.Add("Orange");
      
      // Find the index of the target element
      int index = arrayList.IndexOf("Banana");
      
      // Print the index or a message if not found
      Console.WriteLine(index != -1 ? $"Element found at index {index}" : "Element not found");
      
  8. Passing ArrayList as parameters in C#:

    • Description: This example demonstrates how to pass ArrayList as parameters to methods in C#, enabling modular and reusable code.
    • Code:
      // Method that takes an ArrayList as a parameter
      static void PrintArrayList(ArrayList list)
      {
          foreach (var element in list)
          {
              Console.WriteLine(element);
          }
      }
      
      // Declare and initialize an ArrayList
      ArrayList arrayList = new ArrayList();
      arrayList.Add(1);
      arrayList.Add(2);
      arrayList.Add(3);
      
      // Invoke the method with the ArrayList as a parameter
      PrintArrayList(arrayList);