A Beginner’s Tutorial on Implementing IEnumerable Interface and Understanding yield Keyword

In this article we will discuss about the IEnumerable interface. We will discuss how IEnumerableinterface facilitate the use of foreachstatement to iterate through a set of data. We will then look how to implement our own collections that implement IEnumerableinterface. The use of yield keyword and Enumerating generic collections will also be discussed in this article.

Background

Whenever we work with collection of objects, we might find ourselves in need to iterate the collection. The best way to iterate through a collection is by implementing the Iterator pattern. (refer: Understanding and Implementing the Iterator Pattern in C# and C++[^]). C# provides a very clean construct of foreachstatement to iterate in a read only manner over a collection.

C# also provides us the possibility of using the same foreachconstruct and all the enumeration techniques on our custom collection objects by implementing the IEnumerable interface. So let us see how we can implement IEnumerableinterface with our custom collection classes.

Using the code

Enumerating the Collection classes

Before starting the discussion let us see how we can use the Built-in classes and iterate over them. Lets start by looking into the ArrayListclass that implements IEnumerableand see how we can have read only iteration over that using foreachstatement.

Enumerating the Generic Collection classes

The Arraylistclass is a generalized class that let us keep a collection. We can also have a generic class in which we can provide the type along with the data. Iterating over generic collection classes is also possible because they implement IEnumerable<T> interface. Lets see how we can enumerate over a generic collection.

Now our objective is to have our own custom collection class and a generic collection class that should implement the IEnumerableand IEnumerable<T> interface respectively to provide the possibility of enumerating over them.

Understanding the yield keyword

Before jumping into the Implementation of these classes, we need to understand a very important keyword yieldwhich actually facilitate the enumeration over collection. yield statement is used while returning a value from a function.

A normal method call like the one shown below will return only the first value no matter how many times it is called.

The reason for this is that the normal return statement does not preserve the state of the function while returning. i.e. every call to this function is a new call and it will return the first value only.

Where as if I replace the return keyword by yield return then the function will become capable of saving its state while returning the value. i.e. when the function is called second time, it will continue the processing from where is has returned in the previous call.

When we run the above code it will return 1,2 and then 3. The only catch while using the yieldreturn statement is that the function should return an IEnumerableand should be called from an iteration block i.e. foreachstatement.

Implementing IEnumerable in our custom Collection class

Now in our custom collection classes, if we define a function that will iterate over all the elements in the collection and return then using the yieldkeyword, we will be able to get hold of all the elements in the the collection.

So let us define our own MyArrayListclass and implement IEnumerableinterface, which will force us to implement the GetEnumeratorfunction. This function will iterate over the collection and do a yieldreturn on all the elements.

This class will now let us enumerate all the elements using a foreachstemement.

Note: This class is neither complete not a very good implementation. The only purpose of the sample implementation is to demonstrate the implementation of IEnumerableinterface.

Implementing IEnumerable<T> in our custom Generic Collection class

Let us now take this approach a little further and define a generic collection class capable of being enumerated. To do this we need to implement IEnumerable<T> interface.

This class will now let us enumrate all the elements using a foreachstemement.

So now we have a collection class and a generic collectio class that implement IEnumerableand IEnumerable<T> respectively. Althogh These class is neither complete not a very good implementation but they do serve the purpose of the article i.e. to demonstrate the implementation of IEnumerableinterface.

Point of interest

What we have tried to do in this article is see how can we implement the IEnumerable and IEnumberable<T> interface. We have looked into the significance of yield keyword. This stuff is known to most experienced C# programmers but the beginners might find it useful. I hope this has been informative

Download sample code for this article: EnumerableDemo