I need to move backwards through an array, so I have code like this:
for (int i = myArray.Length - 1; i >= 0; i--)
{
// Do something
myArray[i] =
I'd use the code in the original question, but if you really wanted to use foreach and have an integer index in C#:
foreach (int i in Enumerable.Range(0, myArray.Length).Reverse())
{
myArray[i] = 42;
}
I prefer a while loop. It's more clear to me than decrementing i
in the condition of a for loop
int i = arrayLength;
while(i)
{
i--;
//do something with array[i]
}