Expressions are evaluated from left to right - always, regardless of grouping. So this is equivalent to:
a = 5; int lhs = a;
So, after that, a will be 10. But this only happens after a been evaluated for the first operand of the main addition, so the result is 15, not 20.
It is very important to understand that part of the evaluation order does not coincide with priority. Consider this:
int x = First() + Second() * Third();
Priority means that multiplication is applied to the results of calling Second() and Third() - but First() is still evaluated first. In other words, this statement is equivalent to:
int lhs = First(); int rhs = Second() * Third(); int x = lhs + rhs;
For more information, see Eric Lippert's blog post on prejudice, associativity, and ordering .
I would strongly advise against writing such code.
Jon Skeet Jan 14 '15 at 7:33 2015-01-14 07:33
source share