Possible Duplicate:
Explaining post-increment in C#
Consider the following C# code:-
int i = 2;
i = i++;
Console.WriteLine(i);
I am getting the output as 2
. Why there is no effect of i = i++
?
Possible Duplicate:
Explaining post-increment in C#
Consider the following C# code:-
int i = 2;
i = i++;
Console.WriteLine(i);
I am getting the output as 2
. Why there is no effect of i = i++
?
Depending on where you put the +
-operators, the value assigned is incremented before or after:
i = ++i;
This way i
is counted up before assigned.
i = i++;
This way i
is counted up after assigned.
Because the =
operator takes precedence first.
MSDN: Operator precedence and associativity.
Try this one:
int i = 2;
i = ++i; // or write just ++i;
Console.WriteLine(i);