Understanding C# IEnumerable and IEnumerator

Anything that you can iterate over in .NET implements IEnumerable

Colton
The Crazy Coder

--

Photo by Kelly Sikkema on Unsplash

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:

  1. Traverse in different ways, like forwards, backwards, pre-order or in-order etc
  2. Keep track of the above traversal ways on the same collection at once
  3. Traverse different types of aggregate object by using the same way

--

--

Colton
The Crazy Coder

A software engineer who is always at a high level of passion with new techs and a strong willing to share with what I have learned.