For the long time I thought I get it, and I was going to create some puzzles to learn some of my „students“ on the topic of operator precedence in c#. But it came out that I still don't get it right.
Puzzles: What’s the output here?
int a = 0;
int x = --a + a++;
Console.WriteLine(x);
Console.WriteLine(a);
Output:
-2 0
All clear here, I expected this
Next, the problem one:
int b = 0;
int y = b-- + b++;
Console.WriteLine(y);
Console.WriteLine(b);
Output:
-1 0
Well, here I also expected y to be -2… Now I’m trying to apply operator precedence rules and order of evaluation, and not sure I explained it to myself. Read this post again few times today, but still don’t quite get it why is the result here -1? Can someone help with how is the second result evaluated. why and how is it different than the first one?