-1

How to reset an IEnumerator instance in below case? (e.Reset() throws NotImplementedException)

    void Main()
    {
       IEnumerator<string> e = new List<string> { "a", "b", "c" }.Select(o => o).GetEnumerator();

       while( e.MoveNext() ) 
       {
           Console.WriteLine( e.Current );
       }

       if( 
            //some condition
         ) 
       {
           e.Reset();

           while( e.MoveNext() ) 
           {
               //Do something else with e.Current
           }
       }

    }
st4hoo
  • 2,196
  • 17
  • 25
  • 3
    Also: [Why the Reset() method on Enumerator class must throw a NotSupportedException()?](http://stackoverflow.com/questions/1468170/why-the-reset-method-on-enumerator-class-must-throw-a-notsupportedexception) – sloth Mar 01 '13 at 10:20

2 Answers2

2

Because the 'implementation' of the method will look something like this:

public void Reset() {
  throw new NotImplementedException();
}

For reference from MSDN:

The Reset method is provided for COM interoperability. It does not necessarily need to be implemented; instead, the implementer can simply throw a NotSupportedException.

Where, in this case, the exception type used is a deviation of that recommendation.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
0

Looking at the documentation the explanation is this:

The Reset method is provided for COM interoperability. It does not necessarily need to be implemented; instead, the implementer can simply throw a NotSupportedException.

and

Notes to Implementers All calls to Reset must result in the same state for the enumerator. The preferred implementation is to move the enumerator to the beginning of the collection, before the first element. This invalidates the enumerator if the collection has been modified since the enumerator was created, which is consistent with MoveNext and Current.

Jens Kloster
  • 11,099
  • 5
  • 40
  • 54