Distinction between iterator and enumerator
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Iterator and enumerator are closely related ideas, but they are not always interchangeable. "Iterator" is the broader concept of an object that moves through a sequence, while "enumerator" is often a language- or framework-specific interface for read-only sequential traversal, especially in .NET.
Iterator Is The General Pattern
An iterator is an object that knows how to produce items one at a time from a collection or stream. In many languages, the iterator also controls its own traversal state.
Python is a clear example:
Here it is an iterator. It exposes the iteration protocol directly and advances with next.
The broad idea of an iterator includes:
- sequential access
- internal traversal state
- one element at a time production
- sometimes optional mutation support, depending on the language
This is a general design pattern, not one specific API.
Enumerator Is Often The .NET Flavor
In .NET, the specific object used by foreach is usually an enumerator, typically through IEnumerator or IEnumerator<T>.
An enumerator in this sense exposes:
- '
MoveNext()to advance' - '
Currentto read the current item' - optional
Reset()on the non-generic interface, though it is rarely used in modern code
So in .NET terms, an enumerator is the concrete traversal object returned by GetEnumerator.
The Practical Difference
The easiest way to think about the distinction is:
- iterator is the general concept
- enumerator is one particular API style for that concept
In some discussions, people use the two words loosely. In .NET documentation, the distinction is more explicit because the framework literally defines IEnumerable and IEnumerator.
That leads to a useful mental model:
- '
IEnumerable<T>is the sequence' - '
IEnumerator<T>is the stateful cursor moving through that sequence'
The cursor is what actually performs the enumeration.
Read-Only Traversal Versus Richer Navigation
Enumerators are usually simple, forward-only readers. They are designed for sequential traversal, not random access or flexible editing.
That is different from some iterator models in other ecosystems, where an iterator may support richer operations. For example, Java's ListIterator can move forward and backward and may support element updates.
So another practical distinction is that "enumerator" often implies a minimal traversal contract, while "iterator" can refer to a wider family of traversal tools.
foreach Hides The Enumerator
In C#, most developers do not write enumerator code directly because foreach does it for them:
Under the hood, this is using the collection's enumerator.
That means if you implement a custom collection, supporting GetEnumerator is what makes it work naturally with foreach.
The caller sees a sequence. The runtime uses an enumerator behind the scenes.
Why The Distinction Matters
The distinction becomes useful when reading APIs or designing abstractions.
If someone says "this method returns an iterator," they may just mean it returns something you can traverse.
If the API specifically returns an enumerator, that usually signals:
- stateful forward traversal
- one active position at a time
- direct control over advancement
That is a narrower, more concrete promise.
Common Pitfalls
The biggest mistake is assuming the two words are always perfect synonyms. In casual conversation they often are, but in .NET they map to specific roles in the collection API.
Another mistake is confusing the sequence with the traversal object. An IEnumerable<T> is not the same thing as an IEnumerator<T>.
People also sometimes treat enumerators as reusable. In many real implementations, once an enumerator has been consumed, you should get a fresh one from the collection rather than trying to reuse the old stateful cursor.
Finally, remember that most enumerators are forward-only. If you need indexed access or bidirectional navigation, a simple enumerator may not be the right abstraction.
Summary
- Iterator is the broad concept of stepping through a sequence.
- Enumerator is often a specific API object for that process, especially in .NET.
- In .NET,
IEnumerable<T>represents the sequence andIEnumerator<T>represents the moving cursor. - Enumerators are typically forward-only and stateful.
- The terms overlap, but "enumerator" is usually the narrower and more concrete term.

