Take the following code (usable as a Console Application):
static void Main(string[] args)
{
int i = 0;
i += i++;
Console.WriteLine(i);
Console.ReadLine();
}
The result of i
is 0. I expected 2 (as some of my colleagues did). Probably the compiler creates some sort of structure that results in i
being zero.
The reason I expected 2 is that, in my line of thought, the right hand statement would be evaluated first, incrementing i with 1. Than it is added to i. Since i is already 1, it is adding 1 to 1. So 1 + 1 = 2. Obviously this is not what's happening.
Can you explain what the compiler does or what happens at runtime? Why is the result zero?
Some-sort-of-disclaimer: I'm absolutely aware you won't (and probably shouldn't) use this code. I know I never will. Nevertheless, I find it is interesting to know why it acts in such a way and what is happening exactly.
Best Answer
This:
Can be seen as you doing (the following is a gross oversimplification):
What actually happens is more involved than that - take a look at MSDN, 7.5.9 Postfix increment and decrement operators:
Note that due to order of precedence, the postfix
++
occurs before+=
, but the result ends up being unused (as the previous value ofi
is used).A more thorough decomposition of
i += i++
to the parts it is made of requires one to know that both+=
and++
are not atomic (that is, neither one is a single operation), even if they look like they are. The way these are implemented involve temporary variables, copies ofi
before the operations take place - one for each operation. (I will use the namesiAdd
andiAssign
for the temporary variables used for++
and+=
respectively).So, a closer approximation to what is happening would be: