Note: Please note that the code below is essentially non-sense, and just for illustration purposes.
Based on the fact that the right-hand side of an assignment must always be evaluated before it’s value is assigned to the left-hand side variable, and that increment operations such as ++ and -- are always performed right after evaluation, I would not expect the following code to work:
string[] newArray1 = new[] {"1", "2", "3", "4"};
string[] newArray2 = new string[4];
int IndTmp = 0;
foreach (string TmpString in newArray1)
{
newArray2[IndTmp] = newArray1[IndTmp++];
}
Rather, I would expect newArray1[0] to be assigned to newArray2[1], newArray1[1] to newArray[2] and so on up to the point of throwing a System.IndexOutOfBoundsException . Instead, and to my great surprise, the version that throws the exception is
string[] newArray1 = new[] {"1", "2", "3", "4"};
string[] newArray2 = new string[4];
int IndTmp = 0;
foreach (string TmpString in newArray1)
{
newArray2[IndTmp++] = newArray1[IndTmp];
}
Since, in my understanding, the compiler first evaluates the RHS, assigns it to the LHS and only then increments this is to me an unexpected behaviour. Or is it really expected and I am clearly missing something?
It is instructive to see exactly where your error is:
Correct. Clearly the side effect of the assignment cannot happen until after the value being assigned has been computed.
Almost correct. It is not clear what you mean by “evaluation” — evaluation of what? The original value, the incremented value, or the value of the expression? The easiest way to think about it is that the original value is computed, then the incremented value, then the side effect happens. Then the final value is that one of the original or the incremented value is chosen, depending on whether the operator was prefix or postfix. But your basic premise is pretty good: that the side effect of the increment happens immediately after the final value is determined, and then the final value is produced.
You then seem to be concluding a falsehood from these two correct premises, namely, that the side effects of the left hand side are produced after the evaluation of the right hand side. But nothing in those two premises implies this conclusion! You’ve just pulled that conclusion out of thin air.
It would be more clear if you stated a third correct premise:
Clearly this is true. You need to know two things before an assignment can happen: what value is being assigned, and what memory location is being mutated. You can’t figure those two things out at the same time; you have to figure out one of them first, and we figure out the one on the left hand side — the variable — first in C#. If figuring out where the storage is located causes a side effect then that side effect is produced before we figure out the second thing — the value being assigned to the variable.
In short, in C# the order of evaluations in an assignment to a variable goes like this: