-4
    int x=10;
    
    Console.WriteLine(x);
    x=x++;
    Console.WriteLine(x);

Value of x (10) is assigned to x, then the value of x is incremented which internally works as x=x+1.

So effectively:

  1. x=x
  2. x=x+1

That is - x++ increments x after any operations done in the same line.

So when the value is printed why is it not showing 11? Where is the incremented value (step 2) that is assigned to x (implicitly) gone?

variable
  • 8,262
  • 9
  • 95
  • 215

1 Answers1

0

x++ increments x by 1, but returns its original value.

See here: What's the difference between X = X++; vs X++;?

Hope that helps, Ben

benporter
  • 39
  • 5