Loading...


Both IEnumerable and IEnumerator is used to simple iteration over a collection of specified type.
But there is basic conceptual difference between them. So, lets we see what's the difference between them with an example.

We have a list of month like 

List<string> Month = new List<string>();
Month.Add("January");
Month.Add("February");
Month.Add("March");
Month.Add("April");
Month.Add("May");
Month.Add("June");
Month.Add("July");
Month.Add("August");
Month.Add("September");
Month.Add("October");
Month.Add("November");
Month.Add("December");


Now, we will create IEnumerable<string> with the type of string. And parse Month as IEnumerable<string>.

IEnumerable<string> monthList = (IEnumerable<string>)Month; 

And now we are going do iteration monthList like below

foreach (string str in monthList)
{
Console.WriteLine(str);
}
Console.WriteLine("***********************");
foreach (string str in monthList)
{
Console.WriteLine(str);
}
Console.ReadLine();

Output is:

January
February
March
April
May
June
July
August
September
October
November
December
***********************
January
February
March
April
May
June
July
August
September
October
November
December


It means the state of IEnumerable<string> is not maintained here. When we do new foreach with same instance it will display new list of month.


IEnumerator

Now, we are going create IEnumerator<string> with the type of string. And parse Month as IEnumerator<string>.

IEnumerator<string> monthList = (IEnumerator<string>)Month.GetEnumerator();

Here, we used GetEnumerator(). GetEnumerator is used to return an Enumerator that Iterate through the System.Collections.Generic.List<T> type;

And now we are going do iteration monthList like below
while (monthList.MoveNext())
{
Console.WriteLine(monthList.Current);
}
Console.WriteLine("***********************");
while (monthList.MoveNext())
{
Console.WriteLine(monthList.Current);
}
Console.ReadLine();

Output is:

January
February
March
April
May
June
July
August
September
October
November
December
***********************


In the above example we see one list of months is displaying. The IEnumerator<string> maintain its state and if once we pass the current then can not get it again.
And we cannot operate foreach statement with IEnumerator because IEnumerator<T> does not contain the public definition for GetEnumerator.

MoveNext: MoveNext method is used to move next collection.
Current: Current is used to get the currect collection.

But we can reset its operation if required by calling Reset method of IEnumerator. Sets the IEnumerator to its initial position. 

while (monthList.MoveNext())
{
Console.WriteLine(monthList.Current);
}
Console.WriteLine("***********************");
monthList.Reset();
while (monthList.MoveNext())
{
Console.WriteLine(monthList.Current);
}
Console.ReadLine();

Output is:

January
February
March
April
May
June
July
August
September
October
November
December
***********************
January
February
March
April
May
June
July
August
September
October
November
December