我对下面代码的结果感到困惑。为什么在执行这些操作时 'b' 保留一个看似不正确的值?
int a = 0;
int b = 5;
a = b++;
b = b++;
Console.WriteLine("For b = b++; b=" + b.ToString()); // b should be 7 but it's 6
a = 0;
b = 5;
a = b--;
b = b--;
Console.WriteLine("For b = b--; b=" + b.ToString()); // b should be 3 but it's 4
a = 0;
b = 5;
a = b + 1;
b = b + 1;
Console.WriteLine("For b = b++; b=" + b.ToString());
输出
b=6
b=4
b=6
谁能在 C# 中解释这种行为以及它是如何工作的?