Why there is two completely different version of Reverse for List and IEnumerable?

Cyril Gandon picture Cyril Gandon · Sep 12, 2012 · Viewed 9.8k times · Source

For the List object, we have a method called Reverse().
It reverse the order of the list 'in place', it doesn't return anything.

For the IEnumerable object, we have an extension method called Reverse().
It returns another IEnumerable.

I need to iterate in reverse order throught a list, so I can't directly use the second method, because I get a List, and I don't want to reverse it, just iterate backwards.

So I can either do this :

for(int i = list.Count - 1; i >=0; i--)

Or

foreach(var item in list.AsEnumerable().Reverse())

I found it less readable than if I have an IEnumerable, just do

foreach(var item in list.Reverse())

I can't understand why this 2 methods have been implemented this way, with the same name. It is pretty annoying and confusing.

Why there is not an extension called BackwardsIterator() in the place of Reverse() working for all IEnumerable?

I'm very interested by the historical reason of this choice, more than the 'how to do it' stuff!

Answer

Adam Houldsworth picture Adam Houldsworth · Sep 12, 2012

It is worth noting that the list method is a lot older than the extension method. The naming was likely kept the same as Reverse seems more succinct than BackwardsIterator.

If you want to bypass the list version and go to the extension method, you need to treat the list like an IEnumerable<T>:

var numbers = new List<int>();
numbers.Reverse(); // hits list
(numbers as IEnumerable<int>).Reverse(); // hits extension

Or call the extension method as a static method:

Enumerable.Reverse(numbers);

Note that the Enumerable version will need to iterate the underlying enumerable entirely in order to start iterating it in reverse. If you plan on doing this multiple times over the same enumerable, consider permanently reversing the order and iterating it normally.