Getting an enumerator for the entire ArrayList in C#?

To get an enumerator for the entire ArrayList in C#, you use the GetEnumerator() method. This method returns an IEnumerator object that allows you to iterate through all elements in the ArrayList sequentially.

An enumerator is a read-only, forward-only iterator that provides access to each element in a collection. Unlike a foreach loop, using an enumerator gives you explicit control over the iteration process.

Syntax

Following is the syntax for getting an enumerator for an ArrayList −

IEnumerator enumerator = arrayList.GetEnumerator();
while (enumerator.MoveNext()) {
    Console.WriteLine(enumerator.Current);
}

Using GetEnumerator() with String Elements

Example

using System;
using System.Collections;

public class Demo {
    public static void Main() {
        ArrayList list = new ArrayList();
        list.Add("A");
        list.Add("B");
        list.Add("C");
        list.Add("D");
        list.Add("E");
        
        Console.WriteLine("Elements using foreach:");
        foreach (string element in list) {
            Console.WriteLine(element);
        }
        
        Console.WriteLine("\nElements using GetEnumerator():");
        IEnumerator enumerator = list.GetEnumerator();
        while (enumerator.MoveNext()) {
            Console.WriteLine(enumerator.Current);
        }
        
        // Demonstrating after modification
        list.RemoveAt(2);
        Console.WriteLine("\nAfter removing element at index 2:");
        IEnumerator modifiedEnum = list.GetEnumerator();
        while (modifiedEnum.MoveNext()) {
            Console.WriteLine(modifiedEnum.Current);
        }
    }
}

The output of the above code is −

Elements using foreach:
A
B
C
D
E

Elements using GetEnumerator():
A
B
C
D
E

After removing element at index 2:
A
B
D
E

Using GetEnumerator() with Numeric Elements

Example

using System;
using System.Collections;

public class Demo {
    public static void Main() {
        ArrayList numbers = new ArrayList();
        numbers.Add(100);
        numbers.Add(200);
        numbers.Add(300);
        numbers.Add(400);
        numbers.Add(500);
        
        Console.WriteLine("Count of elements: " + numbers.Count);
        Console.WriteLine("Enumerator iterating the ArrayList:");
        
        IEnumerator enumerator = numbers.GetEnumerator();
        while (enumerator.MoveNext()) {
            Console.WriteLine(enumerator.Current);
        }
    }
}

The output of the above code is −

Count of elements: 5
Enumerator iterating the ArrayList:
100
200
300
400
500

How It Works

The GetEnumerator() method works through three key components −

  • GetEnumerator() − Returns an IEnumerator object positioned before the first element

  • MoveNext() − Advances to the next element and returns true if successful, false if at the end

  • Current − Gets the current element at the enumerator's position

ArrayList Enumerator Process ArrayList GetEnumerator() IEnumerator Iteration Methods enumerator.MoveNext() enumerator.Current

Comparison: foreach vs GetEnumerator()

foreach Loop GetEnumerator() Method
Simpler syntax, automatic iteration Explicit control over iteration process
Compiler handles enumerator internally Manual management of enumerator object
Cannot pause or control iteration flow Can implement custom iteration logic
Read-only access to elements Read-only access to elements

Conclusion

The GetEnumerator() method provides explicit control over ArrayList iteration by returning an IEnumerator object. While foreach loops are more convenient for simple iteration, using GetEnumerator() directly gives you fine-grained control over the enumeration process when needed.

Updated on: 2026-03-17T07:04:36+05:30

163 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements