I don't believe this is possible by conventional methods, but something like this verbose code:
For Each s As String In myStringList Step -1 //' Do stuff here Next
I will probably have to invert the myString object before a conventional For..Each Loop, correct?
Let's discuss certain ways in which this can be done. Method #1 : Using reversed() The simplest way to perform this is to use the reversed function for the for loop and the iteration will start occurring from the rear side than the conventional counting.
It's fundamentally impossible because the foreach loop is based on the Iterable and Iterator interfaces, and those don't have a notion of reverse iteration. An excellent example of this would be infinite sequences. Since there does not exist a "last" element, it is mathematically impossible to reverse the order.
I think the documentation referenced in Mike's answer below is extremely misleading. The order of For Each
is defined by the collection it's called (i.e. its implementation of IEnumerable
/IEnumerable<T>
), but that's not the same as saying it shouldn't be used when the order is important. Many collections (such as arrays, List<T>
etc) always go in the "natural" order.
Part of the documentation does allude to this:
Traversal Order. When you execute a For Each...Next loop, traversal of the collection is under the control of the enumerator object returned by the GetEnumerator method. The order of traversal is not determined by Visual Basic, but rather by the MoveNext method of the enumerator object. This means that you might not be able to predict which element of the collection is the first to be returned in element, or which is the next to be returned after a given element.
That's not at all the same as saying it can't be relied upon - it can be relied upon if you know that the collection you're iterating over will produce the results in the desired order. It's not like it's going to pick elements at random. The behaviour in terms of IEnumerable
/IEnumerable<T>
is clearly defined on that page.
The most important exceptions to predictable orderings are dictionaries and sets, which are naturally unordered.
To reverse an IEnumerable<T>
, use Enumerable.Reverse
- but if you need to iterate in reverse over a collection which is indexed by position (such as an array or List<T>
) then it would be more efficient to use a For
loop starting at the end and working backwards.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With