Pre- & Post Increment in C#
Solution 1:
x--
will be 4, but will be 3 at the moment of --x
, so it will end being 2, then you'll have
x = 4 - 2
btw, your first case will be x = 4 + 6
Here is a small example that will print out the values for each part, maybe this way you'll understand it better:
static void Main(string[] args)
{
int x = 4;
Console.WriteLine("x++: {0}", x++); //after this statement x = 5
Console.WriteLine("++x: {0}", ++x);
int y = 4;
Console.WriteLine("y--: {0}", y--); //after this statement y = 3
Console.WriteLine("--y: {0}", --y);
Console.ReadKey();
}
this prints out
x++: 4
++x: 6
y--: 4
--y: 2
Solution 2:
Lets have a look at the IL that gets generated from that statement
IL_0002: ldloc.0
Loads the value of x onto the stack. Stack => (4)
IL_0003: dup
Duplicates the topmost item on the stack. Stack => (4, 4)
IL_0004: ldc.i4.1
Push 1 onto the stack. Stack => (1, 4, 4)
IL_0005: sub
Subtract the two top values and push result onto the stack. Stack => (3, 4)
IL_0006: stloc.0
Store the topmost value of the stack back to x. Stack => (4)
IL_0007: ldloc.0
Load the value of x back into the stack. Stack => (3, 4)
IL_0008: ldc.i4.1
Load the value 1 onto the stack. Stack => (1, 3, 4)
IL_0009: sub
Subtract the two. Stack => (2, 4)
IL_000A: dup
Duplicate the top value => (2, 2, 4)
IL_000B: stloc.0
Store the top value back to x. Stack => (2, 4)
IL_000C: sub
Subtract the two top values. Stack => (2)
IL_000D: stloc.0
Store this value back into x. x == 2
Solution 3:
From your comment:
I thought that post- and pre-increments are executed after / before evaluation of the complete codeline - but they are executed after / before the evaluation of each item in the expression.
Your misunderstanding is an extremely common one. Note that in some languages, like C, it is not specified when the side effect becomes visible and it is therefore legal, but not required, for your statement to be true in C.
This is not the case in C#; in C# side effects of code on the left side of an expression are always observed to happen before code on the right side executes (from a single thread; in multithreaded scenarios all bets are off.)
For a more detailed explanation of what the increment operators do in C#, see:
What is the difference between i++ and ++i?
There are a great many additional links there to articles I've written on this often-misunderstood topic.
Solution 4:
The most interesting thing that you'll get a completely different answer with C++.Net compiler.
int x = 4;
x = x++ + ++x; // x = 11
x = 4;
x = x-- - --x; // x = -1
Of course the difference in results is determined by different semantics - it seems normal. But despite the understanding the fact that two .net compilers don't behave in a similar manner for such basic things confuses me too.
Solution 5:
In this example,
int x = 4;
x = x++ + ++x;
you can break it like:
x = 4++; which is = 5
x = 4 + ++5; which is 4 + 6
x = 10
Similarly,
int x = 4;
x = x-- - --x;
Here,
x = 4--; which is = 3
x = 4 - --3; which is 4 - 2
x = 2
Simply putting you can say, replace the current value of x, but for every ++ or -- add/subtract a value from x.