Consider this loop:
int[] myArray = new int[10];
int myIndex = 0;
for (int i = 0; i < 10; i++)
{
myArray[myIndex++] = myIndex;
Console.WriteLine(myArray[i]);
}
This yields:
1
2
3
...
Because myIndex is post-incremented, and the right side is evaluated first, shouldn’t the array index 0 contain 0?
Can someone explain this order-of-operations misunderstanding for me?
The right side isn’t necessarily evaluated first. Similar to:
In the above code,
foo.Baris evaluated first, thena + b, then theset_Bazmethod is called to set the Baz property to whatever is evaluated on the right.So in your code, if you break it into pieces, it looks like this: