Iterator Pattern in C#
The Iterator Pattern provides a way to access elements of a collection one by one, without exposing how the collection is organised internally. It lets you move through a set of items — whether it's a list, a tree, or something custom — using a consistent interface.
Real-Life Analogy: Elevator Stopping at Each Floor
Think of an elevator in a building. It goes from floor to floor, in order. You press the button and it takes you to each floor — without needing to know how the elevator system works inside.
The elevator handles the movement. You just tell it to go to the next or previous floor. That's what the Iterator Pattern does in code — it steps through items without showing how they're stored.

Benefits of Iterator Pattern
This pattern separates how we move through a collection from the collection itself. You can change the underlying structure — from a list to a set, for example — and still use the same way to loop through the items.
It supports multiple independent traversals and keeps data structure details hidden from users.
- Uniform access to different collections
- Keeps collection structure private
- Supports multiple or customised iteration styles
When Should You Use It?
Use the Iterator Pattern when you need to loop through elements but don't want the caller to know or care how the data is stored. It's helpful when you may swap out the collection structure in the future.
Perfect for situations where:
- Multiple clients iterate the same data differently
- You want to encapsulate traversal logic
- Your data structure changes but external code must not break
When Not to Use It: Don't use it if the data is simple and iteration logic is fixed — it may add unnecessary abstraction.
What to Implement
To use this pattern, you need:
A collection that can create an iterator, and an iterator object that knows how to go through the collection, one item at a time.
- Iterator Interface: Has methods like HasNext() and Next()
- Concrete Iterator: Implements movement through the collection
- Collection Interface: Creates iterators
- Concrete Collection: Stores items
How It Works in C#
// Iterator Interface
public interface IFloorIterator
{
bool HasNext();
int Next();
}
// Collection Interface
public interface IElevatorShaft
{
IFloorIterator CreateIterator();
}
// Concrete Collection
public class Elevator : IElevatorShaft
{
private readonly List<int> _floors = new() { 1, 2, 3, 4, 5 };
public IFloorIterator CreateIterator() => new FloorIterator(_floors);
}
// Concrete Iterator
public class FloorIterator : IFloorIterator
{
private readonly List<int> _floors;
private int _position = 0;
public FloorIterator(List<int> floors) => _floors = floors;
public bool HasNext() => _position < _floors.Count;
public int Next() => _floors[_position++];
}
Usage:
var elevator = new Elevator();
var iterator = elevator.CreateIterator();
while (iterator.HasNext())
{
Console.WriteLine($"Now stopping at floor {iterator.Next()}");
}
Final Thoughts
The Iterator Pattern is like pressing elevator buttons — you get access to each floor, step by step, without needing to know how the machinery inside works.
It gives your code a clean way to move through collections, keeping logic separate and reusable.