-1

I have this code :

 static void Main(string[] args)
    {
        int a = 1;
        int b = 1;
        int c;

        //task A
        c = a;
        Console.WriteLine(c);
        c = a++;
        Console.WriteLine(c);
        Console.WriteLine(c);


        //task B
        c = b;
        Console.WriteLine(c);
        c = b++;
        Console.WriteLine(c);
        c = b;
        Console.WriteLine(c);

        Console.ReadLine();  

    }

the output is:

task A

1 1 1

task B

1 1 2

Task A question

I expected the result from task A are 1 1 2 because while at second write resulting 1 because post increment, but shouldn't it become 2 now at third write?

Task B question

I expected the result from task B are 1 1 1 because although at second part it is post increment, but shouldn't it become 1 at third write because int b = 1 and c = b?

Thanks.

wendy
  • 161
  • 3
  • 14

4 Answers4

3

In your first case, this line is important:

c = a++;

That will work in two steps:

  1. Assign current value of a to c (c is now 1)
  2. Increment the value of a by one (a is now 2)

Since the value of a isn’t assigned to c after it’s being incremented, c doesn’t know about that new value and remains 1.

In your second case, you do assign a to c after a was incremented. So in the last step, c gets the value of the new a which is 2.

Keep in mind that a post (or pre) increment changes that variable. So in c = a++, a changes after the line is executed, making all future references to a use that incremented value.

poke
  • 369,085
  • 72
  • 557
  • 602
1

Task A

The result is 1 1 1 because a becomes 2 after the assignment, and c retains the assigned value, which is 1.

1) a = 1 , c = 1

2) a = 2 , c = 1 (a is post-icremented)

3) a = 2 , c = 1 (c is not changed)

Task B

The result is 1 1 2 because the second assignment sets c = 1 (b's value before the increment), while the third assignment sets c = 2 (b's value after the increment from the previous assignment)

1) c = 1 , b = 1

2) c = 1 , b = 2 (b is post-incremented)

3) c = 2 , b = 2 (c is assigned b's value)

Mariano D'Ascanio
  • 1,202
  • 2
  • 16
  • 17
1

Here's what happens in detail:

int a = 1;
int b = 1;
int c;

//task A
c = a;   // c is now 1
c = a++; // a is 1 at assignment so c is 1
         // after assignment, a becomes 2; this does not impact c

//task B
c = b;   // c is now 1
c = b++; // b is 1 at assignment so c is 1
         // after assignment, b becomes 2; this does not impact c
c = b;   // c is now 2

The main point to understand is that when you assign a primitive to a variable, it is assigned the primitive's value. In case of objects, both variables would point to the same object, but in case of primitives, the value is copied. Any modifications on variable 1 will not be reflected in variable 2.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
0

Is the expected behavior: a++ will give you the value from a, the increment will be done after. ++a is what you are looking for. Also, native variables (int, char, float, double) get value assignment, not reference with the equal operator.

celerno
  • 1,367
  • 11
  • 30
  • I understand in a++ increment will be done later and that's why I put second write at task A to see it become 2 but the problem it's not. I understand ++a but the problem is I want to understand a++ perfectly. – wendy Jul 09 '14 at 01:28