C# 编译器行为问题?
大家好,下面的代码中,第二个表达式之后 d 的结果应该是什么?
int d = 1;
d += d++;
之后人们会假设 d 为 3,但一元增量 d++ 似乎没有生效,并且 d 保留值 2。
这个 bug 有名称吗?是否存在其他支持一元增量(如 C#)的编译器?
Hey everyone, in the following code, what should the result of d be after the second expression?
int d = 1;
d += d++;
One would assume d is 3 afterwards but the unary increment d++ doesn't seem to take effect and d retains a value of 2.
Is there a name for this bug? Does it exist for other compilers that support unary increment like C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这不是一个错误,它的行为完全符合预期。
+= 运算符扩展为:
这意味着当结果分配回变量时,++ 运算符引起的更改将被覆盖。
It's not a bug, it acts exactly as expected.
The += operator expands into this:
That means that the change that the ++ operator causes is overwritten when the result is assigned back to the variable.
如果你看一下生成的 IL,你就会明白为什么结果是 2 而不是 3。
换句话说:最终存储的值是 1 和 1 的和。
(上面的代码来自发布模式构建)
If you take a look at the generated IL, you'll see why the result is 2 and not 3.
In other words: The final value stored is the sum of 1 and 1.
(the code above is from release mode build)
如果您以这种方式重写代码,它将把 d 设置为 3:
查看 ++ Operator 文档,以解释为什么您的示例的行为方式如此。
摘抄:
正如 @Guffa 指出的,这不是一个错误,只是
d
中后缀增量操作的结果被+=
操作覆盖。If you rewrite your code this way, it will set d to have a value of 3:
Take a look at the ++ Operator documentation for an explanation as to why your example behaves the way it does.
Excerpt:
As @Guffa pointed out, it's not a bug, it's simply that the result of your postfix increment operation in
d
is being overwritten by the+=
operation.我经常收到有关 ++ 运算符“损坏”的问题;几乎总是因为提出问题的人习惯了某些行为 ++ 没有明确定义的语言(例如 C++)中的工作方式。这是我最近写的关于这种情况的文章:
http://blogs.msdn.com/ericlippert/archive/2009/08/10/precedence-vs-order-redux.aspx
I frequently get questions about the ++ operators being "broken"; almost always it is because the person asking the question is used to the way it works in some language where behaviour ++ is not well-defined, like C++. Here's a recent article I wrote about such a scenario:
http://blogs.msdn.com/ericlippert/archive/2009/08/10/precedence-vs-order-redux.aspx
你尝试过++d吗? d++不是之后评估的吗?
Have you tried ++d? Isn't d++ evaluated after?
我认为 d++ 的行为与 ++d 不同,尽管存储在 d 中的最终结果是相同的。
I think behavior of
d++
is different than++d
, though the final result stored ind
is the same.愚蠢的代码是不可预测的。我可以推荐一下吗
Silly code is unpredictable. Might I recommend
d++ 和 ++d 是不同的。也称为“选择未损坏。”
d++ and ++d are different. Also known as "Select Isn't Broken."
...这就是为什么我发现后/前递增/递减运算符在与其他运算符一起用于表达式时非常不可读的原因的一个例子。您描述的行为是正确的,但很难推理,导致误解和错误。
尽管它比较冗长,但我会将其重写为:
请注意使用前增量运算符而不是后增量运算符,以便编译器认为它不需要保留旧值的副本。
...and this would be an example of the reason why I find the post/pre- increment/decrement operators to be highly unreadable when used in an expression with other operators. The behavior you describe is correct, but hard to reason about, leading to misunderstandings and bugs.
Even though it's wordier I would rewrite this as:
Note the use of the pre-increment operator instead of the post-increment so that the compiler doesn't think it needs to keep a copy of the old value.