y = x++
will assign first and then increment. y = ++x
will increment first, then assign. In your example, you are first assigning y
the value of x
, then incrementing x
to 11
.
As int
is a value type (compared to a reference type), changing the value of x
won't affect the value assigned to y
.
int x = 10;
int y = x++; // Assigns the value of x to y (i.e. 10), THEN incrementing x to 11.
Console.WriteLine(x); // Writes the current value of x (i.e. 11).
Console.WriteLine(y); // Writes the current value of y (i.e. 10).