Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 894
JSBձոգչ Avatar asked Oct 20 '09 17:10

JSBձոգչ


2 Answers

When you are designing a framework, you have to make decisions about doing stuff at different abstraction levels. The trade-off is that if you choose to expose things at a high abstraction level, you'll achieve generalization at the cost of losing fine grained control over things. If you choose to expose stuff at lower abstraction levels, your concept will cannot be generalized as well but you can control details at a lower level.

It's a design decision. Implementing both will be costly and make framework more bloated and you'll need to support both when adding features. You'll need to preserve backward compatibility in the future. It's not a wise thing to add everything you can think of to the BCL without making sure it has a significant benefit.

like image 136
mmx Avatar answered Sep 19 '22 10:09

mmx


  • 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.
like image 21
Amy B Avatar answered Sep 19 '22 10:09

Amy B