前& C# 中的后置增量
我对 C# 编译器如何处理前后递增和递减有点困惑。
当我编写以下代码时:
int x = 4;
x = x++ + ++x;
x
之后的值为 10。我认为这是因为预增量将 x
设置为 5
,这使得 5+5
的计算结果为 10
代码>.那么后置增量会将x
更新为6
,但是这个值不会被使用,因为10
将被分配给x
。
但是当我编码时:
int x = 4;
x = x-- - --x;
那么 x
之后将是 2
。谁能解释为什么会这样?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
x--
将是 4,但在--x
时刻将是 3,所以它最终将是 2,那么你将得到顺便说一句,你的第一个案例将是
x = 4 + 6
这是一个小示例,它将打印出每个部分的值,也许这样您会更好地理解它:
这会打印出来
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 + 6
Here is a small example that will print out the values for each part, maybe this way you'll understand it better:
this prints out
让我们看一下从该语句生成的 IL
将 x 的值加载到堆栈中。堆栈=> (4)
复制堆栈最顶层的项目。堆栈=> (4, 4)
将 1 压入堆栈。堆栈=> (1, 4, 4)
将顶部的两个值相减并将结果压入堆栈。堆栈=> (3, 4)
将栈顶的值存回到x。堆栈=> (4)
将x的值加载回堆栈。堆栈=> (3, 4)
将值 1 加载到堆栈中。堆栈=> (1,3,4)
将两者相减。堆栈=> (2, 4)
复制顶部值 => (2, 2, 4)
将最高值存储回x。堆栈=> (2, 4)
减去两个最高值。堆栈=> (2)
将此值存回到x 中。 x == 2
Lets have a look at the IL that gets generated from that statement
Loads the value of x onto the stack. Stack => (4)
Duplicates the topmost item on the stack. Stack => (4, 4)
Push 1 onto the stack. Stack => (1, 4, 4)
Subtract the two top values and push result onto the stack. Stack => (3, 4)
Store the topmost value of the stack back to x. Stack => (4)
Load the value of x back into the stack. Stack => (3, 4)
Load the value 1 onto the stack. Stack => (1, 3, 4)
Subtract the two. Stack => (2, 4)
Duplicate the top value => (2, 2, 4)
Store the top value back to x. Stack => (2, 4)
Subtract the two top values. Stack => (2)
Store this value back into x. x == 2
从你的评论来看:
你的误解是一种极其常见的误解。请注意,在某些语言(例如 C)中,未指定副作用何时可见,因此在 C 中您的语句为 true 是合法的,但不是必需的。
在 C# 中情况并非如此;在 C# 中,表达式左侧代码的副作用总是在右侧代码执行之前发生(来自单个线程;在多线程场景中,所有的赌注都被取消了。
)关于 C# 中增量运算符的详细解释,请参阅:
i++ 和 ++i 有什么区别?< /a>
这里有很多额外的链接,指向我写的关于这个经常被误解的主题的文章。
From your comment:
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.
最有趣的是,使用 C++.Net 编译器你会得到完全不同的答案。
当然,结果的差异是由不同的语义决定的 - 这似乎很正常。但是,尽管我知道两个 .net 编译器对于这些基本事情的行为方式并不相似,但我也很困惑。
The most interesting thing that you'll get a completely different answer with C++.Net compiler.
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.
在这个例子中,
你可以像这样打破它:
类似地,
这里,
简单地说,你可以说,替换 x 的当前值,但对于每个 ++ 或 -- 从 x 中添加/减去一个值。
In this example,
you can break it like:
Similarly,
Here,
Simply putting you can say, replace the current value of x, but for every ++ or -- add/subtract a value from x.
我认为对 ++ + ++ 情况的解释是错误的:
command...........value of x
........................undefined
int x=4 .......4
x++.........................5 (第一个被加数为 4)
++x......................... .....6 (第二个被加数为 6)
x=summand1+summand2 ..4+6=10
类似 -- - -- 情况的解释是
命令............的值x
......................未定义的
整数 x=4 ........................4
x--........................3(减数为 4)
--x.........................2(减数为 2)
x=减法-减数 ..4-2=10
I think the explanation for the ++ + ++ case is wrong:
command...........value of x
..................undefined
int x=4 ..........4
x++...............5 (first summand is 4)
++x...............6 (second summand is 6)
x=summand1+summand2 ..4+6=10
Analogous the explanation for the -- - -- case is
command...........value of x
..................undefined
int x=4 ..........4
x--...............3 (subtactor is 4)
--x...............2 (subtrahend is 2)
x=subtractor-subtrahend ..4-2=10