C# List GetEnumerator() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
List

Definition

In C#, List.GetEnumerator() returns a small helper object, an enumerator, that remembers where you are in the list. You ask it to move forward with MoveNext(), and read the current item with Current.

The foreach statement calls this method for you, so most programs never need to. For List<T> the enumerator is a struct called List<T>.Enumerator, which avoids creating an extra object on the heap. Every enumerator also checks a version number: if the list changes while you are looping, the next MoveNext() throws InvalidOperationException.

This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.GetEnumerator(...). A List<T> is a class, so two variables can share one list. It keeps items in the order you put them, allows duplicates, and grows when needed. An array, by comparison, has a fixed length.

Syntax

Call it, then loop with MoveNext() until it returns false. Most code uses foreach instead.

C#
List<string> names = new List<string> { "Asha", "Ben" };
List<string>.Enumerator e = names.GetEnumerator();
while (e.MoveNext()) {
    Console.WriteLine(e.Current);
}
e.Dispose();
CallWhat it does
names.GetEnumerator()Returns a new enumerator that starts before the first item.
e.MoveNext()Moves to the next item. Returns false when there are no more.
e.CurrentThe item the enumerator points at. Valid only after a MoveNext() that returned true.

Because List<T>.Enumerator is a struct, keep it in one local variable and do not copy it. A copy has its own position, which causes confusing bugs. Boxing it into an IEnumerator<T> works, but it creates a heap object. The enumerator also implements IDisposable, and foreach disposes it for you.

Library declarations
C#
public List<T>.Enumerator GetEnumerator();

Parameters

ParameterDescription
NoneGetEnumerator() takes no parameters.

Return Value

Return ValueDescription
List<T>.EnumeratorA struct enumerator positioned before the first item. Use MoveNext() and Current on it.

C# Examples

Example 1 — Walk a List by Hand

Use MoveNext and Current yourself to do what foreach does.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        List<string> names = new List<string> { "Asha", "Ben", "Chen" };

        List<string>.Enumerator e = names.GetEnumerator();
        while (e.MoveNext()) {
            Console.WriteLine(e.Current);
        }
        e.Dispose();

        foreach (string n in names) {
            Console.Write(n[0]);
        }
        Console.WriteLine();
    }
}

How It Works

  1. Starts before item 0. A new enumerator does not point at anything yet. The first MoveNext() moves it to "Asha" and returns true.
  2. Loop until false. After "Chen", MoveNext() returns false and the loop ends. Current is not used after that.
  3. Same as foreach. The foreach statement does the same steps for you, including the Dispose() call at the end. That is why it is the usual choice.
  4. First letters. n[0] is the first character of each name, written without a line break, so the second loop prints ABC.

Example 2 — Handle the First Item Separately

One manual MoveNext before the loop takes the first item. The loop then handles the rest, which is handy for joining with commas.

C#
using System;
using System.Collections.Generic;
using System.Text;

class Program {
    static void Main() {
        List<int> numbers = new List<int> { 4, 8, 15 };
        StringBuilder sb = new StringBuilder();

        List<int>.Enumerator e = numbers.GetEnumerator();
        if (e.MoveNext()) {
            sb.Append(e.Current);
            while (e.MoveNext()) {
                sb.Append(" and ").Append(e.Current);
            }
        }
        e.Dispose();

        Console.WriteLine(sb.ToString());
    }
}

How It Works

  1. Guard for empty lists. The first MoveNext() is inside an if. If the list were empty, it would return false and nothing would be added.
  2. First item alone. 4 is appended with no text in front of it.
  3. The rest with a prefix. The while continues from the same enumerator, so it starts with 8, and each of those items gets " and " in front.
  4. One enumerator, one position. Both MoveNext() calls use the same variable e, so the loop carries on where the if stopped.

Example 3 — Modify the List While Looping

Removing an item inside a foreach breaks the enumerator. See the error, then fix it with RemoveAll.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        List<int> list = new List<int> { 1, 2, 3, 4 };

        try {
            foreach (int n in list) {
                if (n == 2) {
                    list.Remove(n);
                }
            }
        } catch (InvalidOperationException) {
            Console.WriteLine("cannot change the list inside foreach");
        }

        list.RemoveAll(n => n % 2 == 0);
        Console.WriteLine(string.Join(", ", list));
    }
}

How It Works

  1. The enumerator remembers a version. When the loop starts, the list is at one version. Remove moves the list to a new version.
  2. Next MoveNext fails. The enumerator sees the version no longer matches, and throws InvalidOperationException. The catch block reports it.
  3. The Remove already happened. The 2 was deleted before the exception, so the list was already changed to 1, 3, 4.
  4. Safe fix. RemoveAll removes the even number left (4) without any loop over the list, so the list ends as 1, 3. Looping backwards with RemoveAt also works.

Frequently Asked Questions

It returns an enumerator that walks through the list one item at a time with MoveNext() and Current. The foreach statement uses it behind the scenes.
Almost never. Use foreach. Call it by hand only when you need special control, such as treating the first item differently.
The list keeps a version number that changes on Add, Remove, Insert, and Clear. The enumerator checks it on every MoveNext() and throws InvalidOperationException when it does not match.
It is a struct (a value type) that implements IEnumerator<T>. Being a struct means foreach over a List does not allocate an enumerator object.
Use RemoveAll with a predicate, loop backwards with a for loop and RemoveAt, or loop over a copy made with ToArray().

Next: GetRange()

Copy a block of count items, starting at an index, into a new List.

GetRange() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful