IEnumerable vs. IEnumerator
It all depends on your iteration variable.
Join the DZone community and get the full member experience.
Join For FreeIn this blog, I will try to explain the difference between IEnumerable and IEnumrator by using a simple example. First, we will understand the meaning of both of the terms and why we are using them.
What are IEnumerable and IEnumerator?
namespace System.Collections.Generic
{
//
// Summary:
// Exposes the enumerator, which supports a simple iteration over a collection of
// a specified type.
//
// Type parameters:
// T:
// The type of objects to enumerate.
public interface IEnumerable<out T> : IEnumerable
{
//
// Summary:
// Returns an enumerator that iterates through the collection.
//
// Returns:
// An enumerator that can be used to iterate through the collection.
IEnumerator<T> GetEnumerator();
}
}
The above code explains that IEnumerable internally implements the GetEnumerator() function of IEnumerator. From this, we can say IEnumerable works in the same way as IEnumerator, but there is a big difference between them that we will see with the help of example given below.
Both IEnumerable and IEnumrator are used to iterate through a collection object.
Let's look at this example:
static void Main(string[] args)
{
List<int> IDS = new List<int>();
IDS.Add(1);
IDS.Add(2);
IDS.Add(3);
IDS.Add(4);
IDS.Add(5);
IEnumerable<int> enumerableList = (IEnumerable<int>)IDS;
foreach (var id in enumerableList)
{
Console.WriteLine("id in enumerableList is {0}", id);
}
IEnumerator<int> enumeratorList = IDS.GetEnumerator();
while (enumeratorList.MoveNext())
{
Console.WriteLine("id in enumeratorList is {0}", enumeratorList.Current);
}
Console.ReadKey();
}
In the code block above, we created a List IDS of type integer and added some values to the list. Now, we create an object of IEnumerable<int> as enumerableList and initialize it with list IDS. We follow the same process for the IEnumerator. After creating the objects, we iterate through them. As we can see in the example, IEnumerator has some extra methods to perform operations, such as MoveNext() to find the next element and Current() to get the current value, as the same suggests.
Output for the cases will be the same; the only difference we can see here is we have written more code in IEnumerator for doing the same amount of work.
As we have seen IEnumerable internally implements IEnumerator, so using IEnumerable means we can write less code for the same work of iterating through a list.
Here's the question, "Why are we even using IEnumerator for iterating through any collection?"
Well, IEnumerator maintains the state for an iterating variable.
What does that mean, and what's its use? Modify the above example as:
class Program
{
static void Main(string[] args)
{
List<int> IDS = new List<int>();
IDS.Add(1);
IDS.Add(2);
IDS.Add(3);
IDS.Add(4);
IDS.Add(5);
IEnumerable<int> enumerableList = (IEnumerable<int>)IDS;
foreach (var id in enumerableList)
{
Console.WriteLine("id in enumerableList is {0}", id);
if (id > 3)
{
StateEnumerable(enumerableList);
break;
}
}
IEnumerator<int> enumeratorList = IDS.GetEnumerator();
while (enumeratorList.MoveNext())
{
Console.WriteLine("id in enumeratorList is {0}", enumeratorList.Current);
if (enumeratorList.Current > 3)
{
StateEnumerator(enumeratorList);
break;
}
}
Console.ReadKey();
}
public static void StateEnumerable(IEnumerable<int> enumerableList)
{
foreach(int item in enumerableList)
{
Console.WriteLine("value from stateEnumerable function" + item);
}
}
public static void StateEnumerator(IEnumerator<int> enumerator)
{
while (enumerator.MoveNext())
{
Console.WriteLine("value from stateEnumerator function" + enumerator.Current);
}
}
}
Now, we have added two new methods in our class StateEnumerable(IEnumerable<int> enumerableList) and StateEnumerator(IEnumerator<int> enumerator). We call these methods in the loops of enumerableList and enumeratorList if the value of ID is greater than three.
Here, we are just interested in seeing how the iterating variable (looping variable as "var id in enumerable,id is iterating variable) state behaves in both the cases. Now, run this code and observe the outputs we get.
Output for the above code is :-
So, here is the output, which explains the major difference between IEnumerator and IEnumerable. For a value of four for i, we called the method StateEnumerable function and StsteEnumerator
As IEnumerator is maintaining the state even after passing it, our other function started iterating from five, unlike IEnumerable, which started again from one.
Conclusion:
So it depends on our condition. If we want to iterate through the list (or any collection) at one time, then IEnumerable is the best choice, but if we want to preserve the state of an iteration variable, then we should go with IEnumerator.
Opinions expressed by DZone contributors are their own.
Trending
-
What Is Envoy Proxy?
-
Java String Templates Today
-
Scaling Site Reliability Engineering (SRE) Teams the Right Way
-
What Is Istio Service Mesh?
Comments