2

I am confused by the results of the code below. Why does 'b' retain a seemingly incorrect value when doing these operations?

        int a = 0;
        int b = 5;
        a = b++;
        b = b++;            
        Console.WriteLine("For b = b++; b=" + b.ToString()); // b should be 7 but it's 6
        a = 0;
        b = 5;
        a = b--;
        b = b--;            
        Console.WriteLine("For b = b--; b=" + b.ToString()); // b should be 3 but it's 4
        a = 0;
        b = 5;
        a = b + 1;
        b = b + 1;            
        Console.WriteLine("For b = b++; b=" + b.ToString());

Output

          b=6
          b=4
          b=6

Can anyone explain this behavior in C# and how it's working?

crennie
  • 674
  • 7
  • 18
Abin
  • 2,868
  • 1
  • 23
  • 59
  • 1
    That's the behavior of the [postfix increment operator](http://msdn.microsoft.com/en-us/library/aa691363.aspx), yes. – Frédéric Hamidi Oct 28 '13 at 12:23
  • Most perfect answer probably here: http://stackoverflow.com/questions/13516689/for-i-0-why-is-i-i-equal-to-0/13516748#13516748 – igrimpe Oct 28 '13 at 12:26
  • 2
    Also, it seems fundamentally wrong to assign an increment to itself. `b = b++` does not result in `b += 1`, and `b = ++b` is a waste. – crashmstr Oct 28 '13 at 12:29

4 Answers4

4

That's indeed the behavior of postfix operators, as detailed here.

For instance, when you write:

b = b++;

The following happens:

  1. The current value of b is saved,
  2. b is incremented,
  3. The saved value of b is produced by the postfix ++ operator,
  4. The value produced by the operator is assigned to b.

Therefore, b will indeed be assigned its original value, and the incremented value is lost.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
1

Because the ++ and -- operators when placed after the value will evaluate to the value itself, and then increment/decrement the value after the evaluation.

So:

int a = 0;
int b = a++;

After running this code, b will equal 0 and a will equal 1.

This is as opposed to using the operators as prefixes:

int a = 0;
int b = ++a;

After running this code, b will equal 1 and a will equal 1.

This is documented behavior and has been around for a long time.

David
  • 208,112
  • 36
  • 198
  • 279
0

The instruction a=b++ is stored on the stack but not evaluated because it was not used after that.

To get the correct result, make that instruction have a sense fro example change that line:

Console.WriteLine("For b = b++; b=" + b.ToString());

by that one:

Console.WriteLine("For a = b++; a=" + a.ToString());
Console.WriteLine("For b = b++; b=" + b.ToString()); //should give 7
Houssam Badri
  • 2,441
  • 3
  • 29
  • 60
0

When you use

int a = 0;
int b = 5;
a = b++;
b = b++;

You set a to be 6, and after that you set b to be 6. When you write b to commandline, it presents 6 because a was never used when incrementing b. If you want to use a as well, you'd have to make

int a = 1;
int b = 5;
b = b++;
b += a;
Console.WriteLine("For b = a + b++; b=" + b.ToString());

But overall I don't see any use in this kind of incrementation.

MilanSxD
  • 222
  • 1
  • 7