I am a little confused about how the C# compiler handles pre- and post increments and decrements.
When I code the following:
int x = 4;
x = x++ + ++x;
x will have the value 10 afterwards. I think this is because the pre-increment sets x to 5, which makes it 5+5 which evaluates to 10. Then the post-increment will update x to 6, but this value will not be used because then 10 will be assigned to x.
But when I code:
int x = 4;
x = x-- - --x;
then x will be 2 afterwards. Can anyone explain why this is the case?
x--will be 4, but will be 3 at the moment of--x, so it will end being 2, then you’ll havebtw, your first case will be
x = 4 + 6Here is a small example that will print out the values for each part, maybe this way you’ll understand it better:
this prints out