Introduction
In this article, we will try to bring some light on Enumerator or foreach. What is Better? Both have the same motive, to perform a loop. One allows us to have more control over looping, and the other provides simple syntax. This also brings light to the very familiar concept of the foreach loop. Exactly how foreach loop work, and what is internal processing? Let's first dig into the enumerator.
IEnumerator
IEnumerator is used to go through a list.
IEnumerator has one object type property, i.e., Current, which has getter only and two methods,
- MoveNext returns boolean
- Reset
Code sample
IEnumerator enumerator = enumerable.GetEnumerator();
while (enumerator.MoveNext())
{
object element = enumerator.Current;
// Perform your logic logic on the element
}
Let's consider a scenario where you have to loop through a collection in two different methods. Take the example above,
List<int> enumerable = new List<int>();
for (int i = 0; i <= 20; i++)
{
enumerable.Add(i);
}
IEnumerator enumerator = enumerable.GetEnumerator();
Console.WriteLine("FirstLoopMethod start...");
while (enumerator.MoveNext())
{
object element = enumerator.Current;
int num = int(element);
Console.WriteLine(num);
if(num > 10)
{
SecondLoopMethod(enumerator);
}
// you logic here
// Perform your logic logic on the element
}
private void SecondLoopMethod(IEnumerator enumerator)
{
Console.WriteLine();
Console.WriteLine("SecondLoopMethod start...");
while (enumerator.MoveNext())
{
object element = enumerator.Current;
int num = int(element);
Console.WriteLine(num);
// Perform your logic logic on the element
}
}
This is the output you get,
Here you can see in the SecondLoopMethod that the iteration started from where we left it in the first method.
That's the benefit and control of using IEnumerator.
Where if we try this with IEnumerable, then in the second method, it again starts looping from the beginning
Documentation on Enumerator.
IEnumerable / foreach
We can loop through items of the list using IEnumerator too. IEnumerable and IEnumerator are both interfaces in the .net framework.
IEnumerable has just one method declaration i.e. GetEnumerator, which returns an enumerator.
Every object which can be looped through using foreach inherits from IEnumerable directly or indirectly.
You cannot use foreach on classes that do not inherit from IEnumerable
If you have a model and want to loop through its array using foreach, then that class must inherit from IEnumerable
Code sample
foreach(object item in enumerable)
{
// Perform logic on the item
}
When your code executes, the above code becomes.
While debugging the code, I found the following things,
- When the debugger was at `enumerable` in the above foreach loop, the debugger move to GetEnumerator() method in class
- When the debugger was in a keyword in loop MoveNext method of the Enumerator class was called
- When the debugger was at the item, it loaded the object in it
IEnumerator enumerator = enumerable.GetEnumerator();
while (enumerator.MoveNext())
{
object item = enumerator.Current;
// Perform logic on the item
}
Documentation for Enumerable.
Now what are the benefits and drawbacks of both?
IEnumerable simplifies and shortens the original looping syntax compared to IEnumerator
IEnumerator provides better flexibility and control over your looping logic
Cheers!