views:

277

answers:

5

It's been asked a couple of times on SO how you can implement a bidirectional enumerator (http://stackoverflow.com/questions/191788/two-directional-list-enumerator-in-net, http://stackoverflow.com/questions/451099/implementing-a-bidirectional-enumerator-in-c). 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, arrays). So why does no such type exist?

+3  A: 

Because either nobody thought about it, or nobody thought that it would be particularly useful or because there was not enough budget or...

It's not really necessary, is it? You can easily implement it yourself. Maybe the BCL team thought it was not worth the pain of implementing, testing, documenting etc. Never underestimate the cost of a feature, it may sound "easy", but it does have costs.

Particulary because a single interface that nobody implements would seem strange, wouldn't it? You would expect List, Array etc. to implement the interface, which is quite a lot of work in the end.

Maximilian Mayerl
Heh, like ICloneable. =P
Erik Forbes
As Raymond Chen would say, all features start with -100 points.
Ron Warholic
+1  A: 

Also, what would be the motivation for this? Language support for "backward" iteration?

The iterator pattern doesn't give much weight to the concept of "directionality" of a set of elements. It's a simple pattern for providing a simple interface for iterating over a set.

Gurdas Nijor
+2  A: 
  • 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.
David B
This is not completely true. C# only requires the `MoveNext` method and the `Current` property to work. It doesn't rely on the interface at all.
Mehrdad Afshari
Actually `IEnumerator<T>` isn't needed for `foreach`. As long as your class has a method `GetEnumerator()` that takes no parameters and returns an object which has an `object Current { get; }` property and a `void MoveNext()` method, then `foreach` will work quite happily with it.
Greg Beech
Greg: Really?! So the IEnumerator is effectively duck-typed? That's a revelation.
JSBangs
It's not duck-typed. C# simply check if the type implements the interface OR has the needed methods/properties. The IL that is produced for a foreach loop doesn't use IEnumerable anywhere, so no need to duck-type.
Maximilian Mayerl
That's what I meant by duck typing. It's compile-time duck typing, true, but it still quacks like a duck.
JSBangs
+2  A: 

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.

Mehrdad Afshari
+1  A: 

Obviously, there are many collection types which can't implement this, as MoveNext() is destructive or changes the state of the underlying collection.

MoveNext() is non-destructive. In fact, if the state of the underlying collection is changed between the time that the IEnumerator was created and the time MoveNext() is called, the call to MoveNext() will fail.

The purpose of IEnumerator is to iterate over all of the items in a collection once, in the collection's native order if the collection has a native order. IEnumerator is not intended as a collection-navigation device, such as one might find in C++.

Justice