Rectangle 27 1

c i = i++ doesn't increment i. Why?


Had you used ++i instead, the right-hand side expression would have evaluated to 1, resulting in i == 1.

The key difference is between ++i (pre-increment, which evaluates to the new value of i after incrementing) and i++, or post-increment, which evaluates to the current value of i before incrementing.

Well, the right-hand side expression must be evaluated before the assignment can take place. Now, i++ will evaluate to the current value of i, and i's value will subsequently increase by one. However, the assignment hasn't been performed yet, and when it is, it will overwrite the current value of i (1) with whatever the rhs expression evaluated to, which in your case was 0.

Note
Rectangle 27 1

c i = i++ doesn't increment i. Why?


i==0
i==1
  • Left side i is evaluated to the variable i
  • The assignment is executed, it sets i to 0. (now i==0)

@A-b i = ++i would result in i being 1 in the end. But I'm not sure what you mean by work fine, since this question has to specific purpose the code need to achieve.

Ah! It makes sense. I'm silly :)

In Java you can occasionally observe the value 1 from a parallel thread if you make i volatile (in my test typically 65 times in 100 million iterations).

In general you first create an expression tree. To evaluate it you evaluate first the left side, then the right side and finally the operation at the root. Do that recursively.

The behavior is well defined in C# and the evaluation order is:

The post -increment operator i++ operator is defined as incrementing i but returning the value it had before. The pre -increment operator ++i on the other hand returns the incremented value. i++ => temp=i;i=i+1;return temp; vs. ++i => i=i+1;return i;

i = ++i should work fine as well.

Note
Rectangle 27 1

c i = i++ doesn't increment i. Why?


i++;

@Marc I find the formulation that an expression is evaluated left-to-right confusing. I prefer thinking about the expression as a tree which is then evaluated recursively. And each each node first evaluate its arguments left-to-right and finally executes itself.

I was aware of the difference of preincrement and postincrement. I was confused because I expected the side-effect of increment to take place after the evaluation of the whole expression... Obviously I was wrong

The result of the post-increment expression i++ is the original value of i. So after i++ has been evaluated (incrementing i), you assign the old value of i to ... i.

Note
Rectangle 27 1

c i = i++ doesn't increment i. Why?


i==0
i==1
  • Left side i is evaluated to the variable i
  • The assignment is executed, it sets i to 0. (now i==0)

@A-b i = ++i would result in i being 1 in the end. But I'm not sure what you mean by work fine, since this question has to specific purpose the code need to achieve.

Ah! It makes sense. I'm silly :)

In Java you can occasionally observe the value 1 from a parallel thread if you make i volatile (in my test typically 65 times in 100 million iterations).

In general you first create an expression tree. To evaluate it you evaluate first the left side, then the right side and finally the operation at the root. Do that recursively.

The behavior is well defined in C# and the evaluation order is:

The post -increment operator i++ operator is defined as incrementing i but returning the value it had before. The pre -increment operator ++i on the other hand returns the incremented value. i++ => temp=i;i=i+1;return temp; vs. ++i => i=i+1;return i;

i = ++i should work fine as well.

Note
Rectangle 27 1

c i = i++ doesn't increment i. Why?


i++;

@Marc I find the formulation that an expression is evaluated left-to-right confusing. I prefer thinking about the expression as a tree which is then evaluated recursively. And each each node first evaluate its arguments left-to-right and finally executes itself.

I was aware of the difference of preincrement and postincrement. I was confused because I expected the side-effect of increment to take place after the evaluation of the whole expression... Obviously I was wrong

The result of the post-increment expression i++ is the original value of i. So after i++ has been evaluated (incrementing i), you assign the old value of i to ... i.

Note
Rectangle 27 1

c i = i++ doesn't increment i. Why?


@Armen sure; I understand that. I only mean in the context of the first line of this answer (hence quotes, albeit paraphrased)

@Marc: Except that I'm not looking for any code :)

I don't think he is looking for any code, but he's looking for a better understanding of the C# language. In particular in which order side effects happen and how the result of multiple assignments to the same variable within an expression behaves.

The prefix (i.e. ++i) has a higher periority than the postfix (i.e. i++)

With i++, the value of i is increased, but the value of i++ is not the new value of i, it's the previous value. So when you do i = i++, you're saying "increase the value of i, then set i to the old value".

i = ++iis the code that does what you think is going on here. i++ actually behaves a bit differently.

re the first line; just i++; would be the "code he is looking for"

Note
Rectangle 27 1

c i = i++ doesn't increment i. Why?


@Armen sure; I understand that. I only mean in the context of the first line of this answer (hence quotes, albeit paraphrased)

@Marc: Except that I'm not looking for any code :)

I don't think he is looking for any code, but he's looking for a better understanding of the C# language. In particular in which order side effects happen and how the result of multiple assignments to the same variable within an expression behaves.

The prefix (i.e. ++i) has a higher periority than the postfix (i.e. i++)

With i++, the value of i is increased, but the value of i++ is not the new value of i, it's the previous value. So when you do i = i++, you're saying "increase the value of i, then set i to the old value".

i = ++iis the code that does what you think is going on here. i++ actually behaves a bit differently.

re the first line; just i++; would be the "code he is looking for"

Note