Can Someone please explain why the C# ++ operator provides different outputs based on the assigned variables. https://msdn.microsoft.com/en-us/library/36x43w8w.aspx
int x = 0;
int y = x++;
Console.WriteLine($"x: {x}" );
Console.WriteLine($"y: {y}" );
/*Prints
* x: 1
* y: 0
*/
My understanding is y
is being set to x
, which is (0) then after the assignment of y
; x
is incremented by 1;
int x = 0;
x = x++;
Console.WriteLine($"x: {x}" );
/*Prints
* x: 0
*/
How come the same logic does not applies here? x
should be set to x
, which is 0 then after the assignment increment x
by 1 and print 1 instead of 0
same for this example
int x = 0;
x += x++;
Console.WriteLine($"x: {x}" );
/*Prints
* x: 0
*/
One more example
int x = 0;
x += x++ + x++;
Console.WriteLine($"x: {x}" );
/*Prints
* x: 1
*/
Seems that there is some different logic happening in the background that I am not understanding.