Difference Between IEnumerator And IEnumerable

This blog answers the common interview questions related to IEnumerator and IEnumerable like, how IEnumerator works, what's special about foreach, and why can't I use foreach on my generic classes.

Here is the documentation on Enumerator.
 
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 that returns boolean
  • Reset
Code sample
  1. IEnumerator enumerator = enumerable.GetEnumerator();  
  2.   
  3. while (enumerator.MoveNext())  
  4. {  
  5.     object element = enumerator.Current;  
  6.     // Perform your logic logic on the element  
  7. }  
Let's consider a scenario where you have to loop through a collection in two different method take the example above,
  1. List<int> enumerable = new List<int>();  
  2. for (int i = 0; i <= 20; i++)  
  3. {  
  4.    enumerable.Add(i);  
  5. }  
  6.   
  7. IEnumerator enumerator = enumerable.GetEnumerator();  
  8.   
  9. Console.WriteLine("FirstLoopMethod start...");  
  10. while (enumerator.MoveNext())  
  11. {  
  12.     object element = enumerator.Current;  
  13.     int num = int(element);  
  14.     Console.WriteLine(num);  
  15.     if(num > 10)  
  16.     {  
  17.        SecondLoopMethod(enumerator);  
  18.     }  
  19.   
  20.     // you logic here  
  21.     // Perform your logic logic on the element  
  22. }  
  23. private void SecondLoopMethod(IEnumerator enumerator)  
  24. {  
  25.     Console.WriteLine();  
  26.     Console.WriteLine("SecondLoopMethod start...");  
  27.     while (enumerator.MoveNext())  
  28.     {  
  29.        object element = enumerator.Current;  
  30.       int num = int(element);  
  31.       Console.WriteLine(num);  
  32.       // Perform your logic logic on the element  
  33.    }  
  34. }  
This is the output you get,

Here you can see in the SecondLoopMethod 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 to this with IEnumerable then in the second method it again starts looping from the beginning
Documentation for Enumerable.
 
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 return an enumerator.
 
Every object which can be looped through using foreach, inherits from IEnumerable directly or indirectly.
You cannot use foreach on classes which 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
  1. foreach(object item in enumerable)  
  2. {  
  3.     // Perform logic on the item  
  4. }  
When your code executes the above code becomes.
While debugging the code I found following things
  • When debugger was at `enumerable` in above foreach loop the debugger move to GetEnumerator() method in class 
  • When debugger was at in keyword in loop MoveNext method of Enumerator class was called
  • When debugger was at item it loaded the object in it 
  1. IEnumerator enumerator = enumerable.GetEnumerator();  
  2.   
  3. while (enumerator.MoveNext())  
  4. {  
  5.     object item = enumerator.Current;  
  6.     // Perform logic on the item  
  7. }  
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