Home
C#
GetEnumerator Method
Updated Aug 21, 2024
Dot Net Perls
GetEnumerator. Some .NET collection types offer the GetEnumerator method. This method returns an Enumerator object that can be used to loop through the collection.
Complete knowledge of the actual collection type is not needed. We can use GetEnumerator before a while-loop, and call MoveNext on each iteration.
interface
IEnumerable
This program shows how the GetEnumerator method on the List type works. On a List of ints, GetEnumerator returns a List(int) Enumerator object.
And This object implements IEnumerator(int). We can then write methods that receive IEnumerator(int).
List
Thus With GetEnumerator, it becomes possible to write methods that act on the IEnumerator interface.
Info LinkedList returns LinkedList(int) Enumerator—a LinkedList could also be passed to the Write method.
LinkedList
Dictionary
Important We only require one Write method to handle all generic enumerators that handle integers.
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // Create list of ints.
        List<int> list = new List<int>();
        list.Add(1);
        list.Add(5);
        list.Add(9);

        // Call GetEnumerator on List.
        List<int>.Enumerator e = list.GetEnumerator();
        Write(e);
    }

    static void Write(IEnumerator<int> e)
    {
        // Use the IEnumerator object.
        while (e.MoveNext())
        {
            int value = e.Current;
            Console.WriteLine(value);
        }
    }
}
1 5 9
GetEnumerator benchmark. We can write methods that work on IEnumerator, but is this efficient in .NET 7 in 2022? This benchmark tests GetEnumerator performance.
Version 1 This version of the code uses MoveNext on an IEnumerator interface to sum up a List of integers.
Version 2 Here we use foreach on a List of integers directly and sum up all the values. The result is the same as version 1.
Result Even with .NET 7 in 2022, it is faster to use simpler looping constructs like for and foreach over enumerators.
using System;
using System.Collections.Generic;
using System.Diagnostics;

class Program
{
    static int CountMoveNext(IEnumerator<int> e)
    {
        int count = 0;
        while (e.MoveNext())
        {
            int value = e.Current;
            count += value;
        }
        return count;
    }

    static int CountForeach(List<int> e)
    {
        int count = 0;
        foreach (int value in e)
        {
            count += value;
        }
        return count;
    }

    const int _max = 1000000;
    static void Main()
    {
        List<int> list = new List<int>();
        list.Add(1);
        list.Add(5);
        list.Add(9);
        List<int>.Enumerator e = list.GetEnumerator();

        var s1 = Stopwatch.StartNew();
        // Version 1: use MoveNext.
        for (int i = 0; i < _max; i++)
        {
            var result = CountMoveNext(e);
        }
        s1.Stop();
        var s2 = Stopwatch.StartNew();
        // Version 2: use foreach.
        for (int i = 0; i < _max; i++)
        {
            var result = CountForeach(list);
        }
        s2.Stop();
        Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns"));
        Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns"));
    }
}
26.71 ns CountMoveNext 14.79 ns CountForeach
Summary. GetEnumerator is an instance method provided on many collection types. A unified method can be used to loop over the Enumerator returned by all these collections.
CharEnumerator
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Aug 21, 2024 (edit).
Home
Changes
© 2007-2025 Sam Allen