21 Jan 2020

IEnumerable and IEnumerator

IEnumerable and IEnumerator both are interfaces in C#.

IEnumerable is an interface defining a single method GetEnumerator() that returns an IEnumerator interface.

IEnumerator has two methods MoveNext() and Reset(). It also has a property called Current.


1. Foreach and Enumerables

Unlike for loop, foreach loop is not specifiying the exact index A foreach statement is basically implemented by IEnumerable and IEnumerator.

A Common for loop statement:

    int[] arr = new int[] {1,2,3};
    
    for(int i = 0;i<arr.Length;i++)
    {
        Console.WriteLine(arr[i]);
    }  

Output:

   1
   2
   3

A Common foreach statement:

    int[] arr = new int[] {1,2,3};
    
    foreach(var item in arr)
    {
        Console.WriteLine(item);
    }  

Output:

   1
   2
   3

With IEnumerable and IEnumerator

    int[] arr = new int[] {1,2,3};

    IEnumerable enumerator = arr.GetEnumerator();
    while(enumerator.MoveNext())
    {
        Console.WriteLine(enumerator.Current);
    }

Output:

   1
   2
   3

2. Examples

class Demo : IEnumerable, IEnumerator 
{
   // IEnumerable method GetEnumerator()
   IEnumerator IEnumerable.GetEnumerator() 
   {
      throw new NotImplementedException();
   }
   public object Current 
   {
      get { throw new NotImplementedException(); }
   }
   // IEnumertor method
   public bool MoveNext() 
   {
      throw new NotImplementedException();
   }
   // IEnumertor method
   public void Reset() 
   {
      throw new NotImplementedException();
   }
}

End –Cheng Gu


Tags: