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();
Call
What 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
Parameter
Description
None
GetEnumerator() takes no parameters.
Return Value
Return Value
Description
IEnumerator
Enumerator 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);
}
}
}
Output
Ann
Bob
Cara
How It Works
Hidden call. foreach obtains an enumerator for you.
One element. Each iteration binds n.
Order. Index 0, then 1, then 2.
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);
}
}
}
Output
4
5
6
How It Works
Before first. Enumerator starts before index 0.
MoveNext. Advances and returns true while items remain.
Current. Holds the element as object (prints as int).
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;
}
}
}
}
Output
1
3
5
8
stopped at even
How It Works
Scan until even. 1, 3, 5 are odd.
Hit 8. Print it, then break.
9 skipped. Manual control left the loop early.
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.