Understanding C# IEnumerable and IEnumerator
Anything that you can iterate over in .NET implements IEnumerable
Published in
4 min readAug 26, 2021
Introduction
As two of the very old programming concepts that are implemented in most languages nowadays, this guide helps you have a better understanding of the generic IEnumberable and IEnumberator interfaces.
Background
What Is Iterator Design Pattern
Gof definiton:
Provides a way to traverse the elements of an aggregate object without exposing the underlying representation.
An aggregate object can be:
- a data structure
- a collection
- a list
- a sequence of things of the same type
Some of the requirements when iterating the elements in real world might include:
- Traverse in different ways, like forwards, backwards, pre-order or in-order etc
- Keep track of the above traversal ways on the same collection at once
- Traverse different types of aggregate object by using the same way