C# Array GetEnumerator() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In C#, GetEnumerator returns an enumerator object that can walk the array one element at a time with MoveNext and Current.

You rarely call it yourself—a foreach loop calls GetEnumerator behind the scenes. Knowing it helps when you debug iteration or implement similar patterns.

This is an instance member of System.Array. You call it on an array variable with a dot, such as arr.GetEnumerator(...). Arrays in C# have a fixed length after creation; use List<T> when the count must grow.

Syntax

Usually use foreach. The manual form uses GetEnumerator, MoveNext, and Current.

C#
foreach (int n in nums) { }
var e = nums.GetEnumerator();
CallWhat it does
nums.GetEnumerator()Returns an enumerator positioned before the first element.
e.MoveNext()Advances; returns false when finished.

Disposable enumerators should be disposed; foreach handles that for you.

Library declarations
C#
public IEnumerator GetEnumerator();

Parameters

ParameterDescription
NoneGetEnumerator() takes no parameters.

Return Value

Return ValueDescription
IEnumeratorEnumerator over the array’s elements as objects (non-generic).

C# Examples

Example 1 — foreach Uses GetEnumerator

The everyday way to walk an array.

C#
using System;

class Program {
    static void Main() {
        string[] names = { "Ann", "Bob", "Cara" };
        foreach (string n in names) {
            Console.WriteLine(n);
        }
    }
}

How It Works

  1. Hidden call. foreach obtains an enumerator for you.
  2. One element. Each iteration binds n.
  3. Order. Index 0, then 1, then 2.
  4. Safe default. Prefer this over manual MoveNext.

Example 2 — MoveNext and Current

Drive the enumerator yourself for learning.

C#
using System;
using System.Collections;

class Program {
    static void Main() {
        int[] nums = { 4, 5, 6 };
        IEnumerator e = nums.GetEnumerator();
        while (e.MoveNext()) {
            Console.WriteLine(e.Current);
        }
    }
}

How It Works

  1. Before first. Enumerator starts before index 0.
  2. MoveNext. Advances and returns true while items remain.
  3. Current. Holds the element as object (prints as int).
  4. Loop ends. MoveNext returns false after 6.

Example 3 — Stop Mid-Enumeration

Break out when a condition is met.

C#
using System;
using System.Collections;

class Program {
    static void Main() {
        int[] nums = { 1, 3, 5, 8, 9 };
        IEnumerator e = nums.GetEnumerator();
        while (e.MoveNext()) {
            int n = (int)e.Current;
            Console.WriteLine(n);
            if (n % 2 == 0) {
                Console.WriteLine("stopped at even");
                break;
            }
        }
    }
}

How It Works

  1. Scan until even. 1, 3, 5 are odd.
  2. Hit 8. Print it, then break.
  3. 9 skipped. Manual control left the loop early.
  4. foreach break. You can also break from foreach without GetEnumerator.

Frequently Asked Questions

It returns an object that can walk the array with MoveNext and Current.
Usually no. foreach calls it for you.
On Array’s non-generic enumerator, Current is object. Cast when needed.
Some enumerators support Reset; many foreach patterns just get a new enumerator.
Array.ForEach is a static helper that runs an Action. GetEnumerator powers foreach-style iteration.

Next: GetLength()

Return the number of elements in a given dimension (int) with System.Array.

GetLength() 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.

7 people found this page helpful