C# — IEnumerable and IEnumerator interfaces

Oleg Tsibulevskiy
Just Eat Takeaway-tech
3 min readAug 17, 2020

Review structure and building custom enumerator.

Photo by Andrew Buchanan on Unsplash

IEnumerable and IEnumerator interfaces, thanks to them we can iterate over the elements in a loop, let’s go look how.

Element

We will use the Restaurant class as elements in the examples:

IEnumerable

The IEnumerable interface defines only one method GetEnumerator()which returns an enumerator that iterates through a collection through implementing the interface IEnumerator. IEnumerator we will review it later, for now, let’s look at the implementation of the IEnumerable interface.

All that we need for this is to create a class that will store a list of elements and implement IEnumerable, create an instance, and iterate over it in a foreach loop.

As shown, we created a class Restaurants which implements IEnumerable. For simplicity, we created a list of restaurants inside the class and we’re using the non-generic types, but you can pass the list to the constructor and use generic version of interface IEnumerable<T>. The method GetEnumerator() returns the built-in enumerator from the restaurants list, we can do this because List implements the IEnumerable as well. To test our code we created an instance of Restaurants object and run a foreach loop.

All this is cool, but what if we don’t want to use the default implementation of the IEnumerator interface and we need to add some of our own logic? For this, we need to review the IEnumerator interface.

IEnumerator

IEnumerator is an interface which lets you get the Current element from a collection. It has two methods:

1. MoveNext - Advances the enumerator to the next element of the collection. It returns true if the enumerator was successfully set to the next element and false if the enumerator has reached the end of the collection.

2. Reset - Sets the enumerator to its initial position, which is before the first element in the collection.

Knowing this, we can now create a custom enumerator. For example, we want to iterate over only opened restaurants and IEnumerator can help us with this. First, we need to create a class that will get a list of elements and implement interface IEnumerator.

The class contains the properties _restaurants and _position.

_position - Needed for storing the current position
_restaurants - Needed for storing the list of restaurants

The positions start from -1 because the first method that will be called isMoveNext(), which moves the position forward by one. Only then can we access the Current property to get the element at the current position.
The MoveNext() method will increase the position pointer and check if the next restaurant is open until it gets to the end of the list.

Now we need to create and return an instance of our enumerator from the class implemented IEnumerable interface GetEnumerator() method.

Conclusion

Sure, it looks like a lot of code to iterate over restaurants, but your logic could be more complicated and you might want to hide that behind a class. In that case, implementing a custom IEnumerable and IEnumerator can be a great solution.

Make sure to check out our careers page to discover tech jobs at Takeaway.com!

--

--