C# IEnumerable Overview

Today in this section, we'll see IEnumerable<T> in action and will see how it actually works and holds true for all collections. As I already said in one of my articles that collections don't iterate themself, they need enumerators to loop through them and its IEnumerable<T>'s job to provide the enumerators. So whenever we loop through any collection using foreach(), we are actually invoking IEnumarable<T> to get the enumerator. So, IEnumerable<T> exposes only one method and that is GetEnumerators().

Here, enumerator uses three basic properties to iterate in the list and that is MoveNext(), Reset and Current. MoveNext() will iterate until the collection ends and Current will set the subsequent item to the current one. Now, let's see that in action.

  1. using System.Collections;    
  2. using System.Collections.Generic;    
  3. using System.Collections.ObjectModel;    
  4. using System.Linq;    
  5. using System;    
  6.     
  7. namespace Collections    
  8. {    
  9.     internal class Stack    
  10.     {    
  11.         private static void Main(string[] args)    
  12.         {    
  13.             string[] monthsofYear =    
  14.                             {    
  15.                                 "January",    
  16.                                 "February",    
  17.                                 "March",    
  18.                                 "April",    
  19.                                 "May",    
  20.                                 "June",    
  21.                                 "July",    
  22.                                 "August",    
  23.                                 "September",    
  24.                                 "October",    
  25.                                 "November",    
  26.                                 "December"    
  27.                             };    
  28.     
  29.             ShowItems(monthsofYear);    
  30.             Console.ReadLine();    
  31.         }    
  32.     
  33.         public static void ShowItems<T>(IEnumerable<T> coll)    
  34.         {    
  35.             //IEnumerator implements IDisposable, hence wrapped in a using block    
  36.             //    
  37.             using (IEnumerator<T> enumerator = coll.GetEnumerator())    
  38.             {    
  39.                 bool nextItem = enumerator.MoveNext();    
  40.                 //when nextItem will become false, looping will stop    
  41.                 while (nextItem)    
  42.                 {    
  43.                     T item = enumerator.Current;    
  44.                     Console.WriteLine(item);    
  45.                     nextItem = enumerator.MoveNext();    
  46.                 }    
  47.             }    
  48.         }    
  49.     }    
  50.     
  51.     

output

Now, consider the scenario of foreach(). As shown below in the example foreach() is pretty straightforward, I could have used it in the Main method directly as I was using in the previous examples. However, I just wanted to emphasize the one point that foreach() internally replaces the code with with the previous example of IEnumerator<T>.

  1. using System.Collections;    
  2. using System.Collections.Generic;    
  3. using System.Collections.ObjectModel;    
  4. using System.Linq;    
  5. using System;    
  6.     
  7. namespace Collections    
  8. {    
  9.     internal class Stack    
  10.     {    
  11.         private static void Main(string[] args)    
  12.         {    
  13.             string[] monthsofYear =    
  14.                             {    
  15.                                 "January",    
  16.                                 "February",    
  17.                                 "March",    
  18.                                 "April",    
  19.                                 "May",    
  20.                                 "June",    
  21.                                 "July",    
  22.                                 "August",    
  23.                                 "September",    
  24.                                 "October",    
  25.                                 "November",    
  26.                                 "December"    
  27.                             };    
  28.     
  29.             //ShowItems(monthsofYear);    
  30.             ShowItems2(monthsofYear);    
  31.             Console.ReadLine();    
  32.         }    
  33.     
  34.         public static void ShowItems<T>(IEnumerable<T> coll)    
  35.         {    
  36.             //IEnumerator implements IDisposable, hence wrapped in a using block    
  37.             //    
  38.             using (IEnumerator<T> enumerator = coll.GetEnumerator())    
  39.             {    
  40.                 bool nextItem = enumerator.MoveNext();    
  41.                 //when nextItem will become false, looping will stop    
  42.                 while (nextItem)    
  43.                 {    
  44.                     T item = enumerator.Current;    
  45.                     Console.WriteLine(item);    
  46.                     nextItem = enumerator.MoveNext();    
  47.                 }    
  48.             }    
  49.         }    
  50.     
  51.         public static void ShowItems2<T>(IEnumerable<T> coll)    
  52.         {    
  53.             foreach (T item in coll)    
  54.             {    
  55.                 Console.WriteLine(item);    
  56.             }    
  57.         }    
  58.     }    
  59.     
  60.     
  61. }  

And of course the result will be the same only.

result

Now, there is one exception to this; if the compiler notices that the collection we are iterating through is an Array then it will simply convert the foreach() method to a for loop than IEnumeartor<T> implementation, since that is the most efficient way of iterating. But, this only holds for arrays.

One more point is, enumeration is a read-only operation, so if you try to modify the collection on the fly and iterate through it, this won't allow that.

  1. using System.Collections;    
  2. using System.Collections.Generic;    
  3. using System.Collections.ObjectModel;    
  4. using System.Linq;    
  5. using System;    
  6.     
  7. namespace Collections    
  8. {    
  9.     internal class Stack    
  10.     {    
  11.         private static void Main(string[] args)    
  12.         {    
  13.             var monthsofYear = new List<string>    
  14.                             {    
  15.                                 "January",    
  16.                                 "February",    
  17.                                 "March",    
  18.                                 "April",    
  19.                                 "May",    
  20.                                 "June",    
  21.                                 "July",    
  22.                                 "August",    
  23.                                 "September",    
  24.                                 "October",    
  25.                                 "November",    
  26.                                 "December"    
  27.                             };    
  28.     
  29.     
  30.             foreach (var item in monthsofYear)    
  31.             {    
  32.                 monthsofYear[2] = "Financial Month";    
  33.                 Console.WriteLine(item);    
  34.             }    
  35.             Console.ReadLine();    
  36.         }    
  37.     
  38.     
  39.     }    
  40.     
  41.     

exception

However, the preceding restriction is not applicable for all the collections. For example, if I change it to an Array and run it again then the compiler won't complain it. However, one point to note is that it's not a good practice to modify the element while iterating and the reason for this is foreach() for arrays is converted into a for loop, hence the modification is allowed in that.

Thanks for joining me.

Rahul sahay,
Happy Coding

Up Next
    Ebook Download
    View all
    Learn
    View all