Important Update
The Guide Feature will be discontinued after December 15th, 2023. Until then, you can continue to access and refer to the existing guides.
Author avatar

Dániel Szabó

forEach and Enumerables in C#

Dániel Szabó

  • Jan 9, 2020
  • 10 Min read
  • 19,414 Views
  • Jan 9, 2020
  • 10 Min read
  • 19,414 Views
Languages Frameworks and Tools
C#

Introduction

This guide revolves around two very old programming concepts that are implemented in most languages nowadays. One of the concepts is generators and the other is iterators. In this guide, we will inspect how these ideas were incorporated into C# and check out how we can utilize them for our own advantage, considering all the pros and cons.

Generators

A generator is usually a function, or a special routine implemented in the language which controls the behavior of a loop. Generators bear an eerie resemblance to functions that return an array as they have parameters and can be called to generate a sequence of items.

Let's say we have a function that returns a list of integers, about one million at a time. Without generators, the list would need to be built up in memory, allocate place for all the values, and then pass those values wherever they need to go. If you have the resources, that's fine; however, people always strive to figure out something for these kinds of problems, and thus generators were born. A generator will yield one item at a time, allowing the caller to get started processing the first values immediately. This requires less memory and allows more expressive control flows in your applications. This feature has been available since version 2.0 of the language.

Iterators

The concept of an iterator dates back as far as 1974 to the CLU programming language. By design, an iterator should allow the programmer to traverse specific containers. Iterators are most commonly used for lists. They are implemented in C# as interfaces and tightly coupled to the container itself in implementation. An iterator allows access to the data held in the container.

upport for iterators came to the language with version 2.0. In C#, iterators are called enumerators, and they come from the IEnumerator interface. Basically, this interface provides a MoveNext() method, which goes for the next element and checks if the end of container or collection is reached. It also has a Current property that allows access to the value of the element currently pointed at. There is also a Reset() method that allows you to move the enumerator back to its initial position. Before the first element can be retrieved, a MoveNext() call is needed as the enumerator points before the first element. You can obtain an enumerator if the GetEnumerator() method of an object or collection implementing the IEnumerable interface is called.

Note: Most of the container classes implement the interface; however, the foreach loop cannot operate on these.

Implementation

First let me show you a demonstration code. The list class is coming from the System.Collections.Generic namespace.

We create a list.

1List<string> toBeIterated = new List<string>();
2toBeIterated.Add("Programming");
3toBeIterated.Add("is");
4toBeIterated.Add("really");
5toBeIterated.Add("fun");
6toBeIterated.Add("if");
7toBeIterated.Add("you");
8toBeIterated.Add("do");
9toBeIterated.Add("it");
10toBeIterated.Add("right");
csharp

We can convert this list to a generator very simply.

1IEnumerable<string> iEnumerableOftoBeIterated = (IEnumerable<string>)toBeIterated;
csharp

Using a foreach loop will allow us to traverse the list.

1foreach(string element in iEnumerableOftoBeIterated)
2{  
3   Console.WriteLine(element);  
4}  
csharp

Executing the code will give us the following result.

1Programming
2is
3really
4fun
5if
6you
7do
8it
9right
bash

This was the generator approach. Now let's see how we can use the iterator approach.

Let's convert our list.

1IEnumerator<string> iEnumeratorOftoBeIterated = toBeIterated.GetEnumerator();
csharp

As mentioned above, GetEnumerator() will work because the List class from the Generic namespace implements the IEnumerator interface. However, the foreach cannot be used. We need a while loop for this.

1while (iEnumeratorOftoBeIterated.MoveNext())
2{
3	Console.WriteLine($"The current value is: {iEnumeratorOftoBeIterated.Current}");
4}
csharp

Executing the above code will give us the following.

1The current value is: Programming
2The current value is: is
3The current value is: really
4The current value is: fun
5The current value is: if
6The current value is: you
7The current value is: do
8The current value is: it
9The current value is: right
bash

Note that there is no break statement, yet it does not go into infinite mode.

Note: Both of these approaches have the same goal: to allow the programmer to traverse a container, in our case a list. However, when we use the IEnumerator approach, it only works if the MoveNext() function is called. The main difference between IEnumerable and IEnumerator is that the latter retains the cursor's current state.

A word of advice: If you would like tho sequentially go through a collection, you should use the IEnumerable interface. If you would like to retain the cursor position and pass it between functions, you should use IENumerator.

Let's look at a practical example. We would like to create a small app that can process numbers differently depending on whether they are odd or even. IEnumerator provides us an elegant solution that involves two static methods.

1using System;
2using System.Threading;
3using System.Collections.Generic;
4
5namespace generatorsNIterators
6{   
7    class Program
8    {
9        static void EvenProcessor(IEnumerator<int> i)
10        {              
11
12            if (i.Current == 0)
13                {
14                    Console.WriteLine("The list was processed, exiting!");
15                    Thread.Sleep(10);
16                    i.Dispose();
17            }
18            else if (i.Current % 2 != 0)
19                {
20                    Console.WriteLine("The number is ODD, calling processor.");
21                    OddProcessor(i);
22                }
23            else if(i.Current % 2 == 0)
24            {
25                Console.WriteLine($"Processing even: {i.Current}");
26                Console.WriteLine("Even number processed!");
27                i.MoveNext();
28                EvenProcessor(i);
29            }           
30            else
31                { i.Dispose(); }
32        }
33        static void OddProcessor(IEnumerator<int> i)
34        {
35            if (i.Current == 0)
36                {
37                    Console.WriteLine("The list was processed, exiting!");
38                    Thread.Sleep(5);
39                    i.Dispose();
40                }
41            else if (i.Current % 2 == 0)
42                {
43                    Console.WriteLine("The number is EVEN, calling processor.");
44                    EvenProcessor(i);
45                
46            }
47            else if(i.Current % 2 != 0)
48                {
49                    Console.WriteLine($"Processing odd: {i.Current}");
50                    Console.WriteLine("Odd number processed!");
51                    i.MoveNext();
52                    OddProcessor(i);
53                }
54            else
55                { i.Dispose(); }
56        }
57        static void Main(string[] args)
58        {
59            List<int> myList = new List<int>(10);
60            for (int i = 1; i <= 20; i++) { myList.Add(i); }
61            IEnumerator<int> myListEnum = myList.GetEnumerator();
62            myListEnum.MoveNext();
63            OddProcessor(myListEnum);
64            Console.ReadKey();
65            }
66    }
67}
csharp

When we execute this code, the following is on our console.

1Processing odd: 1
2Odd number processed!
3The number is EVEN, calling processor.
4Processing even: 2
5Even number processed!
6The number is ODD, calling processor.
7Processing odd: 3
8Odd number processed!
9The number is EVEN, calling processor.
10Processing even: 4
11Even number processed!
12The number is ODD, calling processor.
13Processing odd: 5
14Odd number processed!
15The number is EVEN, calling processor.
16Processing even: 6
17Even number processed!
18The number is ODD, calling processor.
19Processing odd: 7
20Odd number processed!
21The number is EVEN, calling processor.
22Processing even: 8
23Even number processed!
24The number is ODD, calling processor.
25Processing odd: 9
26Odd number processed!
27The number is EVEN, calling processor.
28Processing even: 10
29Even number processed!
30The list was processed, exiting!
bash

What happens here? We have a list with numbers from 1 to 20, and this list is converted to become an iterator with the following statement.

1IEnumerator<int> myListEnum = myList.GetEnumerator();
csharp

This is needed because we have two static methods, EvenProcessor and OddProcessor. Both take an IEnumerator argument, which is the Current position of our cursor. This solution does not use any loops. It works simply through the magic of iterators. Our list is only 10 items long, and if the number is even the appropriate function will process it. The same is the case with the odd numbers. The key here is the i.Current and the fact that the position of the cursor is kept.

Conclusion

All in all, generators and iterators are, in my opinion, very useful. They allow you to create clean control flow in your applications and yet keep the code clean. In this guide, we defined generators and iterators, exploring where they come from and why. Then we built up our knowledge, ending with a complete demonstration. The coolest part is that we were able to process a list without for, while, or do while loops involved. I hope this has been informative and you found what you were looking for. If you liked this guide, give it a thumbs up and stay tuned for more, or check out my other guides.