Difference between IEnumerator and IEnumerable Interface in C#


IEnumerable and IEnumerator both are interfaces in C#.

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

This works for readonly access to a collection that implements that IEnumerable can be used with a foreach statement.

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

The following shows the implementation of IEnumerable and IEnumerator.

Example

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();
   }
}

Above you can see the two methods of IEnumerator.

// IEnumertor method
public bool MoveNext() {
   throw new NotImplementedException();
}

// IEnumertor method
public void Reset() {
   throw new NotImplementedException();
}

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 23-Jun-2020

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements