Skip to content

Instantly share code, notes, and snippets.

@TheFo2sh
Created March 28, 2020 22:47
Show Gist options
  • Save TheFo2sh/dcf7531ece92fcecae960d958021cf33 to your computer and use it in GitHub Desktop.
Save TheFo2sh/dcf7531ece92fcecae960d958021cf33 to your computer and use it in GitHub Desktop.
public class GeneratorEnumerator<T> : IEnumerator<T>
{
private readonly Func<int,T> _generatorFunc;
private int _index;
private T _current;
public GeneratorEnumerator(Func<int, T> generatorFunc)
{
_generatorFunc = generatorFunc;
}
public bool MoveNext()
{
_current = _generatorFunc(_index++);
return true;
}
public void Reset()
{
_current = default(T);
_index = 0;
}
public T Current => _current;
object IEnumerator.Current => Current;
public void Dispose()
{
_current = default(T);
_index = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment