Iterators in .NET


public interface IEnumerable
{
  public IEnumerator GetEnumerator();
}


public interface IEnumerator
{
  public object Current { get; }
  public bool MoveNext();
  public void Reset();
}
Something is enumerable if it implements IEnumerable, and, well you can guess the rest. Here, querying and advancing are separated - MoveNext advances, returning true if there is another element. Current does the obvious, and can be called multiple times. Reset takes you back to the start. Note the difference in approach to Python - reaching the end of a sequence isn't exceptional.

Note that foreach works with IEnumerable, but that IEnumerator doesn't implement IEnumerable. Consequently, you can't use an IEnumerator in foreach loops. Personally, I think this is a bit silly, because it makes foreach less useful. foreach knows how to work with IEnumerable, and with arrays, so it's a bit of a shame really.

Slightly curiously, these two interfaces are in System.Collections rather than System even though they work with a language keyword.