Is there a way to use a foreach
loop to iterate through a collection backwards or in a completely random order?
Using an IList
from the C5 Generic Collection Library, Reverse iteration is a feature, rather than extension:
foreach (var i in list.Reverse())
{
}
As well, you can use the Shuffle()
method to get a random ordering:
var listClone = (IList) list.Clone();
listClone.Shuffle();
foreach (var i in listClone)
{
}