Friday, April 4, 2014

Difference between IEnumerable and IEnumerator

IEnumerable is an interface that has the definition one method named GetEnumerator which return an object of type IEnumerator.


public class OldEnumerable : IEnumerable
{
  public IEnumerator GetEnumerator()
  {
      return new OldEnumerator();
  }
}



Now let’s look at OldEnumerator


public class OldEnumerator : IEnumerator
{
  int x = 0;


  public object Current
  {
      get { return x; }
  }


  public bool MoveNext()
  {
      return x++ < 100;
  }


  public void Reset()
  {
      x = 0;
  }
}

No comments:

Followers

Link