0

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.

elite_developer
  • 116
  • 1
  • 6
  • Detailed description of how ++ operator (both prefix and postfix) works in C# is here: http://stackoverflow.com/a/3346729/1286670 – Ňuf Feb 07 '17 at 00:54
  • @Steve I'm not sure if it was intentional or not but you changed _"Unary Prefix Increment `++` Operator"_ to _"Unary Prefix Increment `+` Operator"_ - no such thing –  Feb 07 '17 at 01:03
  • I believe Eric Lippert's answer found by @Ňuf covers all concerns of this post. If there something still missing - please [edit] the post and clarify what parts need separate explanation. Please make sure to write exact step-by-step reasoning when making an edit - you should have at most couple extra steps in addition to 5 Eric provided for ++ operation to cover whole `x = x++` statement. – Alexei Levenkov Feb 07 '17 at 01:05
  • @MickyD yeah, my bad. Thought you were talking about removing the language tag from the title, which is appropriate generally. – Steve Feb 07 '17 at 01:07
  • @Steve No worries, yes saw that too. No harm done. Wishing you well :) –  Feb 07 '17 at 01:08
  • 1
    @AlexeiLevenkov is this really a duplicate of http://stackoverflow.com/questions/3346450/what-is-the-difference-between-i-and-i as this question doesn't have anything to do with pre-increment? – Alex Wiese Feb 07 '17 at 01:11
  • @AlexWiese I assume you've read top answers to that question and somehow they did not cover behavior of post-increment for you. I'm definitely not married to this duplicate, but some better reasoning is needed - linked question clearly explains difference between *result* of post increment operation and value of variable which seem to be main part of this question to me. – Alexei Levenkov Feb 07 '17 at 01:16
  • @AlexeiLevenkov Incorrect duplicate –  Feb 07 '17 at 01:16
  • @MickyD that is possible. So far question is based on misunderstanding of result of `x++` operator ("My understanding is y is being set to x..." vs "y is being set to *result of (x++)* ..."). OP can edit the question to clear that part and than indeed it may not be duplicate. Anyone else can edit too, but need to be done carefully to know OP's intentions - asking new question in that case may be better option. – Alexei Levenkov Feb 07 '17 at 01:28
  • ok thanks Alexei :) –  Feb 07 '17 at 01:43

2 Answers2

1
int x = 0;
x  = x++;

First operations - store initial value of x (0), second operation - increase x (1), third operation - assign stored value to x (0)

int x = 0;
x  += x++;

In this case first operand of addition "freezed" before increment (0), second operand "freezed" before increment too (0). As result 0 + 0 = 0

Uladzimir Palekh
  • 1,846
  • 13
  • 16
0

You've got the operator the wrong way around for what you're trying to do.

Per the documentation:

The increment operator can appear before or after its operand: ++variable and variable++

The first form is a prefix increment operation. The result of the operation is the value of the operand after it has been incremented.

The second form is a postfix increment operation. The result of the operation is the value of the operand before it has been incremented.

So in your code sample, you are setting y to x before x is incremented. If you wish to set y to the new value of x you need to use the prefix increment operation to increment x and have the result be the new value to do with as you wish:

int x = 0;
int y = ++x;
Console.WriteLine($"x: {x}" );
Console.WriteLine($"y: {y}" );
/*Prints
  * x: 1
  * y: 1
*/
Community
  • 1
  • 1
Steve
  • 9,335
  • 10
  • 49
  • 81
  • 1
    I think what OP is getting at is that he read the code as `int x=0; x++; x += 0;` which would be equal to 1. – Alex Wiese Feb 07 '17 at 00:59