How exactly is the right way to call IEnumerator.Reset?
The documentation says:
The
Reset
method is provided for COM interoperabi
public class PeopleEnum : IEnumerator
{
public Person[] _people;
// Enumerators are positioned before the first element
// until the first MoveNext() call.
int position = -1;
public PeopleEnum(Person[] list)
{
_people = list;
}
public bool MoveNext()
{
position++;
return (position < _people.Length);
}
public void Reset()
{
position = -1;
}
object IEnumerator.Current
{
get
{
return Current;
}
}
public Person Current
{
get
{
try
{
return _people[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
never; ultimately this was a mistake. The correct way to iterate a sequence more than once is to call .GetEnumerator()
again - i.e. use foreach
again. If your data is non-repeatable (or expensive to repeat), buffer it via .ToList()
or similar.
It is a formal requirement in the language spec that iterator blocks throw exceptions for this method. As such, you cannot rely on it working. Ever.
I recommend not using it. A lot of modern IEnumerable
implementations will just throw an exception.
Getting enumerators is hardly ever "expensive". It is enumerating them all (fully) that can be expensive.