Introduction

This article aims at explaining the Iterator pattern and having a rudimentary implementation of the Iterator pattern in C#. This article is meant for beginners
and does not use any language built-in features for iterations.

Background

Having a collection of objects is a very common thing in software development. If we have a collection of objects then we might also find ourselves in need to traverse this collection.
Most languages provide traversal techniques over basic collection types. C# also contain some special container types capable of holding a collection of values
(example: Lists, and ArrayLists in C#). These specialized containers also come with the possibility of getting iterated.
C# container classes are the best examples of how the iterator pattern is implemented.

Note: We will not be using any of these techniques in this article.

If we want the underlying working mechanism of these iterator objects then we will perhaps need to understand the Iterator pattern first. The idea behind the Iterator pattern
is that we decouple the actual collection object from the traversal logic. This will make the collection object lighter as it does not have to deal with all the
iteration related functionalities and from the user's point of view, there is a clear separation between the collection and how the collection is being iterated.
Also, the user will not have to worry about keeping track of the number of items traversed, remaining, and whether to check for boundary conditions as all this is already
being done in the iterator object (as these things will depend on the underlying structure and implementation of the collection object).

GoF defines the Iterator pattern as "Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation". To
visualize the GoF design (slightly modified as per our implementation):

Using the code

Before jumping on to the implementation, let us try to see what each class in this diagram represents:

IIterator: This is an interface that defines the methods for accessing and traversing elements.

MyIterator: This is ConcreteIterator, this will implement the Iterator interface and keep track of the current position in the traversal of the aggregate object.

IAggregate: This is an interface that defines methods for creating an Iterator object.

MyAggregate: This is the ConcreteAggregate object, i.e., the real collection lies inside this. This class implements the IAggregate creation interface.

Creating the Iterator Interface

So now let us try to implement this pattern by implementing one class at a time. Let us start by writing the IIterator interface. This interface should provide
the methods for accessing and traversing the elements of the collection object.

Now that we have both the interfaces ready, we can now define the concrete class that will hold a collection of objects. Let's have a simple class that will hold
a collection of string values. We will use our iterator to get hold of these string values.

Now we have a simple implementation of the Iterator pattern in place. There are a lot of possible optimizations possible in the code but the intent here is to explain
the Iterator pattern so the code is written in a simple (and perhaps not so efficient) manner.

Points of Interest

The .NET Framework and C# language has the Iterator pattern embedded deep in its code. The IEnumerable interface is in fact the facilitator
of the Iterator pattern. Generics and Collection classes in C# can be iterated through an enumerator which is in fact an Iterator pattern implementation.

We have not seen the C# and .NET specific implementations of the Iterator pattern in this article. Using the language and framework built-in iterators is definitely more
efficient and less error prone. The idea behind this article is to understand how the Iterator pattern works and we implemented a simple Iterator pattern in C# .

Share

About the Author

I Started my Programming career with C++. Later got a chance to develop Windows Form applications using C#. Currently using C#, ASP.NET & ASP.NET MVC to create Information Systems, e-commerce/e-governance Portals and Data driven websites.

My interests involves Programming, Website development and Learning/Teaching subjects related to Computer Science/Information Systems. IMO, C# is the best programming language and I love working with C# and other Microsoft Technologies.

Comments and Discussions

Why are you doing this?
The problem of a sequential access to an aggregate object (a list in the GoFour book) is solved with introduction of STL containers, STL class template Iterator, and STL algorithms. It is very strange to have guys writing the abstract iterator, concrete iterator, etc. Now it is just