The following snippet of C# code:
int i = 1; string result = String.Format('{0},{1},{2}', i++, i++, i++); Console.WriteLine(result);
writes out: 1,2,3
Before I tried this in the compiler I was expecting the assignments to take place and then the evaluations, so my expected output was: 1,1,1
So my question is: Does this ‘pattern’ (is it called a pattern?) of assign and then evaluate each parameter have a name?
EDIT: I’m referring to the pattern of evaluating and assigning the parameters to the String.Format() function. Not the incrementing of i.
(I may be incorrectly using the word evaluate in the question above because if the parameter was say (i + j) then we know that it would be evaluated before it was assigned. When using the word evaluate in that context I’m referring to the incrementing of i.)
The order of evaluation of arguments is strictly left-to-right in C#. When you evaluate the expression
i++, what happens is the value ofiis calculated and pushed, then the value ofiis incremented.The ++ operator on System.Int32 is effectively a function with the special name
++and the special syntax of calling it by writing a reference to a variable and then the characters ++.So in effect, what you wrote is
Since arguments are evaluated left-to-right,
Inc(ref i)is called 3 times, each time incrementingiafter passing the current value ofitoString.Format(...). This is exactly what happens in your code, as well.