C# post increment and pre increment

.net, c#

Solution

Difference is, what the operator returns:

The post-increment operator "a plus plus" adds one, and returns the old value:

int a = 1;
int b = a++;
// now a is 2, b is 1

The pre-increment operator "plus plus a" adds one, and returns the new value:

    a = 1;
    b = ++a;
// now a is 2 and b is 2

Problem

I get very confused at times with the shorthand increment operation. Since when i was little programming in BASIC, i got some how stuck with a = a+1 which is long painful way of saying 'Get a's current value, add 1 to it and then store the new value back to a'. ``` 1] a = a +1 ; 2] a++ ; 3] ++a; 4] a +=1; ``` [1] and [4] are similar in functionality different in notation, right? 2] and 3] work simply differently because of the fact that the increment signs ++ is before and after. Right? Am I safe to assume the below? ``` int f(int x){ return x * x;} y = f(x++) -> for x =2, f(x) = x^2 f(x) ======> y= 2^2 =4 x=x+1; ======> x= 2+1 = 3 y = f(++x) -> for x =2, f(x) = x^2 x=x+1 ===========> x = 2+1 = 3 f(x) ===========> y =3^2 = 9 ```

Original source

Related problems