IEnumerable and IEnumerator in C# with real time example

C# IEnumerable real life example IEnumerator real life example IEnumerable vs IEnumerator interface IEnumerable & IEnumerator convert list to IEnumerable GetEnumerator to convert list into IEnumerable c# implement ienumerable difference between ienumerable and ienumerator using ienumerable ienumerator example c# custom ienumerable c# ienumerable index how to use ienumerable in c# foreach ienumerator

We will discuss here what is IEnumerable, IEnumerator, what are differences between them, where should we use IEnumerable and where to IEnumerator, once we will finish our discussion it will be clear, which one would be best for which situation and why, so let’s see it with example

To better understand we will create a list of age

List<int> ages = new List<int>();
ages.Add(10);
ages.Add(20);
ages.Add(30);
ages.Add(40);
ages.Add(50);

Now convert this list to IEnumerable

IEnumerable<int> age_IEnumerable = (IEnumerable<int>)ages;
foreach (int age in age_IEnumerable)
{
    Console.WriteLine(age);
}

There is nothing new, we used foreach here very straight forward, now let’s convert the ages into IEnumerator, there is a method GetEnumerator to convert a list into IEnumerator

IEnumerator<int> age_IEnumerator = ages.GetEnumerator();
while (age_IEnumerator.MoveNext())
{
    Console.WriteLine(age_IEnumerator.Current);
}

As you can see here we used while rather than foreach because foreach cannot be used with IEnumerator, but still there is nothing which can suggest us when should we use IEnumerable and where to IEnumerator.

Before we go further, we should know, IEnumerable uses IEnumerator internally also have a function GetEnumerator to convert into IEnumerator, we should use IEnumerable because it make coding easier and clearer as we can see in above example.

Now let’s discuss the main difference, IEnumerable doesn’t remember the state, which row or record it is iterating while IEnumerator remember it and we are going to see this with example by creating method PrintUpto30 and PrintGreaterThan30

Let’s first check with IEnumerator

public void PrintAgeUpto30(IEnumerator<int> age_IEnumerator)
{
   while (age_IEnumerator.MoveNext()){
      Console.WriteLine(age_IEnumerator.Current);
      if (age_IEnumerator.Current > 20) {
         Console.WriteLine("PrintGreaterThan30 is called");
         PrintGreaterThan30(age_IEnumerator);
      }
   }
}

public void PrintGreaterThan30(IEnumerator<int> age_IEnumerator)
{
    while (age_IEnumerator.MoveNext())
        Console.WriteLine(age_IEnumerator.Current);
}
// Now Call PrintUpto30 which will call PrintGreaterThan30 
// by using our previous age IEnumerator
PrintUpto30(IEnumerator);

As we know IEnumerator persists state so once we will call PrintGreaterThan30 by passing age_IEnumerator it will print the remaining list, here is the output:

10
20
30
PrintGreaterThan30 is called
40
50

Now let’s check same code with IEnumerable

public void PrintUpto30(IEnumerable<int> age_IEnumerable)
{
    foreach (int age in age_IEnumerable){
       Console.WriteLine(age);
       if (age > 20){
         Console.WriteLine("PrintGreaterThan30 is called");
        PrintGreaterThan30(age_IEnumerable);
      }
   }
}

public void PrintGreaterThan30(IEnumerable<int> age_IEnumerable)
{
  foreach (int age in age_IEnumerable)
    Console.WriteLine(age);
}

// Now Call PrintUpto30 by using our variable age_IEnumerable

PrintAgeUpto30(age_IEnumerable);

Now check the output you will see “PrintGreaterThan30 is called” many times something like this

10
20
30
PrintGreaterThan30 is called
10
....
50
40
PrintGreaterThan30 is called
10
...
50
50
PrintGreaterThan30 is called
10
50

Conclusion:

  1. Both are interface
  2. IEnumerable code are clear and can be used in foreach loop
  3. IEnumerator use While, MoveNext, current to get current record
  4. IEnumerable doesn’t remember state
  5. IEnumerator persists state means which row it is reading
  6. IEnumerator cannot be used in foreach loop
  7. IEnumerable defines one method GetEnumerator which returns an IEnumerator
  8. IEnumerator allows readonly access to a collection
Ali Adravi Having 13+ years of experience in Microsoft Technologies (C#, ASP.Net, MVC and SQL Server). Worked with Metaoption LLC, for more than 9 years and still with the same company. Always ready to learn new technologies and tricks.
  • oops
  • c#
By Ali Adravi On 29 Mar, 13  Viewed: 33,051

Other blogs you may like

Static Class in C# why and when with examples

Static class, static method and static constructor are some common questions which are asked in most interviews. A static class is basically the same as a non-static class, but there in one difference, a static class cannot be instantiated, means we cannot use new keyword to create an object of the... By Ali Adravi   On 01 Jan 2015  Viewed: 6,279

Interface in C# with real time example

In this article we will discuss about interface in C# with real time example, it properties, implementations and pros and cons as well. 1. An interface can never be instantiated e.g. ICar car = new ICar(); It’s wrong. 2. It only contains signature of its methods. 3. An interface has neither... By Ali Adravi   On 18 Aug 2013  Viewed: 21,464

Method overloading in C# with example

Method overloading means having different methods with same name but with different types of parameters or number of parameters also known as static polymorphism. In this article we will try to understand what is method overloading and how we can overload a method with example. Suppose we have a... By Hamden   On 19 Jul 2013  Viewed: 1,378

Polymorphism in C# with example

Polymorphism is derived from two Latin words, 1. Poly means many 2. Marphose means forms. By using inheritance, a class can be used as many types, say we have a Person which may be a Professor, may be a Student, may be Clerk. So in this article we will see some example of Polymorphism. When we... By Hamden   On 01 Jun 2013  Viewed: 2,120

Convert string to an enum type in .net

Sometime we need to convert string to an enum type, say I need to store my enum value into viewstate then we cannot store enum or any object to viewstate so converted that to string and store it, and on postback need to read the viewstate and convert to enum, so how to convert a string into enum?... By Hamden   On 21 May 2013  Viewed: 918