Last post Mar 07, 2014 08:22 AM by skilledres
Mar 07, 2014 12:05 AM|skilledres|LINK
Please absorve the follwoing code once.
int x = 20;
x = x++;
I have a little bit confusion with the post increment in the above statement.
Please explain me why x value is not increment here.
Mar 07, 2014 12:30 AM|Paul Linton|LINK
Because it does what the name says. It increments post (post means after). The value is returned and then after it is incremented.
20 is calculated as the return value, then x is incremented to 21 and then the assignment operator sets x to the returned value of 20.
Mar 07, 2014 12:32 AM|Paul Linton|LINK
Or, put another way, it is incremented but then you overwrite the incremented value with the preincremented value.
Mar 07, 2014 12:58 AM|jsiahaan|LINK
These three statements are the right syntaxes.
x = x + 1;
x += 1;
Again, it's the way syntax to add.
Mar 07, 2014 01:17 AM|Paul Linton|LINK
"These three statements are the right syntaxes."
There is nothing wrong with the syntax in the OP, it is perfectly legal C# syntax.
It is the semantics that the OP has got wrong.
Mar 07, 2014 08:22 AM|skilledres|LINK