Click here to Skip to main content
15,893,401 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
int x = 20;
x = x++;
Console.WriteLine(x); 


In the above statement x=x++;

why x value is not increment.

I have a little bit confusion here. Please explain about this.
Posted
Updated 6-Mar-14 17:56pm
v2
Comments
Sergey Alexandrovich Kryukov 7-Mar-14 0:08am    
Could you explain why writing such thing at all? Just for fun?
—SA
Srinubabu Ravilla 7-Mar-14 0:26am    
Dear Sergey thank you for your reply.
I am not writing it for fun.
I writing to know about whats happening here.
There is difference between
int x=10, y=10;
x=x++;
Console.WriteLine(x); // output: 10
y=x++;
Console.WriteLine(x);// output: 11

Can you explain why x value is increment first time but not second time.
Sergey Alexandrovich Kryukov 7-Mar-14 0:57am    
Very good. So, let's see...
—SA
Aurus_Deepak 7-Mar-14 2:22am    
x= x++, can be broken down to:

1) increment the value contained in x
now x contains 11

2) return the value that was contained in x before it was incremented
that is 10

3) assign that value to x
now, x contains 10
Sergey Alexandrovich Kryukov 7-Mar-14 8:51am    
No, because increment should go after assignment. Look at the decompiled code to see.
—SA

This is pretty clear.

I would ask you: what result did you expected and why? Did you expect 21, as in case of y = x++? No way! Compiler is not supposed and should not make any assumption on the developer's intention, it should follow simple and strict principles. If you do weird thing which makes no practical sense, as in this case, the logical and apparent result may also appear weird.

The compiler needs to estimate expression on right two times, before ++ and after, because this is post-increment. The value "before" is needed for assignment; and value "after" is the result of increment. As x appears in both sides, the addition (temporary) slot on stack is reserved, to manipulate with the expression on right. The x estimated, this is 20. Then 20 is assigned to first slot representing variable x. It becomes 20. Then the memory location representing expression on right needs to be incremented by 1. It becomes 21. On exist from the stack frame, this value is discarded.

This is the only logical interpretation of post-increment with assignment to the same variable. You can easily see how it works if you disassemble this fragment of code.

You should understand that this is the pathological case which makes no sense at all in practice. Increment can be performed by writing ++x or x++. Combination of increment with assignment only makes sense if the result is assigned to a different variable.

I appreciate your interest to some pathological cases. It gives us the idea that some operations are not as trivial as they seem and that caution needs to be used when working with increments.

—SA
 
Share this answer
 
v2
Comments
george4986 7-Mar-14 1:07am    
nice explanation by the way my 5+
thanks for correcting me ;-)
Sergey Alexandrovich Kryukov 7-Mar-14 1:19am    
Thank you, George.
—SA
Srinubabu Ravilla 7-Mar-14 1:20am    
Thank you SA. You cleared my doubt.
Sergey Alexandrovich Kryukov 7-Mar-14 1:23am    
You are very welcome.
Good luck, call again.
—SA
U are using the post increment (x++) so only the value of x is not incremented at first time.
Use the pre increment (++x).

++x increments a before it is evaluated. x++ evaluates a and then increments it.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900