-3

I have been trying to understand the difference between ref and out in C# and faced this misunderstanding of a++ and ++a.

class Program
{
    static void Main ( string [] args )
    {
        int a = 3;
        incr (  a ) ;
        Console.ReadKey ();

    }


    public static void incr (   int a ) // a = 3
    {
        Console.WriteLine (++a); // a incremented to 4
        Console.WriteLine ( a++ ); // a should be incremented to 5 , but it is still 4
    }
}

can anyone explain why a++ didn't increment to 5 in the above code.

Hassan Nahhal
  • 31
  • 1
  • 8

4 Answers4

4
public static void incr (int a ) // a = 3
{
    Console.WriteLine (++a); // Pre-Increment:  Increment to 4 and pass it in.
    Console.WriteLine (a++); // Post-Increment: Increment to 5, but use the old value (4).
    Console.WriteLine (a);   // Will show 5
}

The problem is that a++ will increment to 5, but it will use the old value for the parameter before it was incremented. ++a increments and the new value will be passed into the method.

TyCobb
  • 8,909
  • 1
  • 33
  • 53
  • can you check your code , I think you mean the first `++a` should be `a++` right ? – Hassan Nahhal Jun 28 '15 at 00:12
  • 1
    check out [here](http://stackoverflow.com/questions/3346450/what-is-the-difference-between-i-and-i) @HassanNahhal – M.kazem Akhgary Jun 28 '15 at 00:13
  • @M.kazemAkhgary thanks a lot, i checked all the questions and there was a guy called saying that the answers are wrong with high reputation, i read his answer but it was so hard to understand. – Hassan Nahhal Jun 28 '15 at 00:17
  • @HassanNahhal I see what you are getting at. He is correct and I will reword. Technically it does increment, but uses the old value. – TyCobb Jun 28 '15 at 00:19
2

From the documentation on the ++ operator

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 your code expanded out looks more like this

public static void incr (   int a ) // a = 3
{
    int aResult;

    //++a
    a = a + 1; // a = 4
    aResult = a; //aResult = 4
    Console.WriteLine (aResult ); //  prints 4

    //a++
    aResult = a; //aResult = 4
    a = a + 1; //a = 5
    Console.WriteLine (aResult); // prints 4 because the result was copied before the increment.
}
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
1

a++ is evaluated then assigned. But ++a is Assigned then evaluated.

i = a++;
// a = a + 1;
// i = a

but

i = ++a;
// i = a
// a = a + 1
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
0

That's pre and post increment.

++p -- pre-increment will increment first and then will display in console output

whereas p++ will display in console first and then will increment and so you the result.

So, in your case

Console.WriteLine (++a); // a incremented to 4
Console.WriteLine ( a++ ); // it will display 4
Console.WriteLine ( a ); // it will be 5 this time
Rahul
  • 76,197
  • 13
  • 71
  • 125