Search code examples
c#.netienumeratorenumerators

Why doesn't .NET have a bidirectional enumerator?


It's been asked a couple of times on SO how you can implement a bidirectional enumerator (here, here). My question is not how (which is trivial for most cases), but why no such type exists in the .NET platform.

public interface IBidirectionalEnumerator<T> : IEnumerator<T>
{
    bool MovePrev();
}

Obviously, there are many collection types which can't implement this, as MoveNext() is destructive or changes the state of the underlying collection. But conversely, many types can implement this trivially (List, IList, LinkedList, array).

Why does no such type exist?


Solution

    • IEnumerator supports the c# foreach statement as well as looping constructs of other languages.
    • There is no statement or common programming idiom that IBidirectionalEnumerator enables.