我=我++不增加 i。为什么?
可能的重复:
为什么会进入无限循环?
之类的东西code>i = i++ 在 C 和 C++ 中具有未定义的行为,因为标量对象的值在同一表达式内更改两次,而无需插入序列点。
不过,我认为这些类型的表达式在 C# 或 Java 中具有明确定义的行为,因为据我所知,参数的求值是从左到右的,并且到处都有序列点。
也就是说,我希望 i = i++
等同于 i++
。但事实并非如此。以下程序输出 0
。
using System;
class Program
{
static void Main(string[] args)
{
int i = 0;
i = i++;
Console.WriteLine(i);
}
}
你能帮我理解为什么吗?
免责声明: 我完全意识到,无论是否定义了上述构造的行为,它们都是愚蠢的、无用的、不可读的、不必要的,并且不应该在代码中使用。我只是好奇。
Possible Duplicates:
Why does this go into an infinite loop?
Things like i = i++
have undefined behavior in C and C++ because the value of a scalar object is changes twice within the same expression without intervening sequence point.
However I suppose that these kind of expressions have well-defined behavior in C# or Java because AFAIK the evaluation of argument goes left to right and there are sequence points all over.
That said, I'd expect i = i++
to be equivalent to i++
. But it's not. The following program outputs 0
.
using System;
class Program
{
static void Main(string[] args)
{
int i = 0;
i = i++;
Console.WriteLine(i);
}
}
Could you help me understand why?
Disclaimer:
I am fully aware that whether or not the behavior of above-mentioned constructs is defined, they are silly, useless, unreadable, unnecessary and should not be used in code. I am just curious.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该行为在 C# 中定义良好,计算顺序为:
i
计算为变量i
i
code> 递增(现在i==1
)i
设置为 0。(现在i==0
)最终结果是
i==0
。通常,您首先创建一个表达式树。要评估它,您首先评估左侧,然后评估右侧,最后评估根部的操作。递归地这样做。
The behavior is well defined in C# and the evaluation order is:
i
is evaluated to the variablei
i
is incremented (nowi==1
)i
to 0. (nowi==0
)The end result is
i==0
.In general you first create an expression tree. To evaluate it you evaluate first the left side, then the right side and finally the operation at the root. Do that recursively.
后置自增表达式
i++
的结果是i
的原始值。因此,在对i++
求值后(递增i
),您将i
的旧值分配给 ...i
>。只需使用
;)
The result of the post-increment expression
i++
is the original value ofi
. So afteri++
has been evaluated (incrementingi
), you assign the old value ofi
to ...i
.Just use
;)
i = ++i
是执行您认为此处发生的操作的代码。i++
实际上的行为有点不同。使用
i++
,i
的值增加了,但是i++
的值并不是i
的新值,它是之前的值。因此,当您执行i = i++
时,您是在说“增加i
的值,然后将i
设置为旧值”。i = ++i
is the code that does what you think is going on here.i++
actually behaves a bit differently.With
i++
, the value ofi
is increased, but the value ofi++
is not the new value ofi
, it's the previous value. So when you doi = i++
, you're saying "increase the value ofi
, then seti
to the old value".那么,在进行赋值之前必须先计算右侧表达式。现在,
i++
将计算出i
的当前值,并且 i 的值随后将增加 1。但是,赋值尚未执行,当执行时,它将用 rhs 表达式的计算结果覆盖i
(1) 的当前值,在您的情况下为 0。主要区别在于
++i
(预增量,其计算结果为i
after 增量的新值) 和i++
,或者后增量,其计算结果为增量之前i
的当前值。如果您改用
++i
,则右侧表达式的计算结果将为 1,导致 i == 1。Well, the right-hand side expression must be evaluated before the assignment can take place. Now,
i++
will evaluate to the current value ofi
, and i's value will subsequently increase by one. However, the assignment hasn't been performed yet, and when it is, it will overwrite the current value ofi
(1) with whatever the rhs expression evaluated to, which in your case was 0.The key difference is between
++i
(pre-increment, which evaluates to the new value ofi
after incrementing) andi++
, or post-increment, which evaluates to the current value ofi
before incrementing.Had you used
++i
instead, the right-hand side expression would have evaluated to 1, resulting in i == 1.