c#后递增问题 [英] c# Post-incrementing problem
问题描述
我发现C#有问题并且发布了增量。我通过c ++中的一些源代码发现了
,发现有人做了一个
后增量:
int x = 0;
for(int i = 0; i< 10; i ++)
{
x = x ++;
}
在c ++中,你会得到0,1,......,9(正如预期的那样)。这不是
在C#中工作。在C#中,你得到0,...,0。在这种情况下,似乎忽略了帖子
增量。但是,行:
x = 4;
y = x ++;
产生y = 4,x = 5(正如预期的那样)
c#应该以这种方式运行还是有编译器
错误?在用c ++和c#查看汇编代码之后,它看起来像是c#中的一个bug。
I found a problem with C# and post increments. I was going
through some source code in c++ and found someone did a
post increment:
int x=0;
for(int i=0; i<10; i++)
{
x = x++;
}
In c++, you''d get 0,1,...,9 (as expected). This doesn''t
work in C#. In C#, you get 0,...,0. It seems the post
increment is ignored in this case. However, the lines:
x=4;
y=x++;
produces y=4, x=5 (as expected)
Should c# be behaving this way or is there a compiler
error? After looking at the assembly code in c++ and c#, it
looks like a bug in c#.
推荐答案
开[ GMT + 0100 = CET],
Patrick Wood< pw ********** @ boeing.com>苦思冥想:
On [GMT+0100=CET],
Patrick Wood <pw**********@boeing.com> thought hard and spewed:
我发现了C#的问题并且发布了增量。我用c ++中的一些源代码来发现有人做了一个
后增量:
int x = 0;
for(int i = 0; i< ; 10; i ++)
{
x = x ++;
}
I found a problem with C# and post increments. I was going
through some source code in c++ and found someone did a
post increment:
int x=0;
for(int i=0; i<10; i++)
{
x = x++;
}
我试过这段代码并确认它没有增量x。必须是一个bug。
I tried this code and confirmed it doesn''t increment x. Must be a bug.
On [GMT + 0100 = CET],
Patrick Wood< pw ******** **@boeing.com>苦思冥想:
On [GMT+0100=CET],
Patrick Wood <pw**********@boeing.com> thought hard and spewed:
我发现了C#的问题并且发布了增量。我用c ++中的一些源代码来发现有人做了一个
后增量:
int x = 0;
for(int i = 0; i< ; 10; i ++)
{
x = x ++;
}
I found a problem with C# and post increments. I was going
through some source code in c++ and found someone did a
post increment:
int x=0;
for(int i=0; i<10; i++)
{
x = x++;
}
但是,减去后期,预先计算和预先减少工作循环。
只有postincrement失败。
However, post decrement, preincrment, and predecrement work inside the loop.
Only postincrement fails.
引人入胜。
我不得不说, C ++行为(肯定会发生什么)是
*不是我所期望的。 C#似乎是正确的。
x ++应增加x的值但返回旧值。因此,在第一次通过循环时,x ++将x增加到1,但返回
旧值为0.将此0分配给x应该在*之后*
++,所以x应保持为零,就像在C#中一样。
假设我们将y声明为另一个int。
用以下代码替换循环体:
y = x ++;
x = y;
给出x保持0的行为,因为在增量之后肯定会发生
的分配。
现在这里很奇怪:
x = y = x ++;
第一次循环,y得0,x得1.我怀疑
有一个优化发生,导致C ++行为错误。
问候,
贾斯珀肯特
***通过开发人员指南 http://www.developersdex.com ***
不要只是一部分在USENET中的icipate ...获得奖励!
Fascinating.
I have to say, the C++ behaviour (which is certainly what happens) is
*not* what I would expect. The C# seems correct.
x++ should increment the value of x but return the old value. Thus on
the first time through the loop, x++ increments x to 1, but returns the
old value of 0. The assignment of this 0 into x should occur *after* the
++, and so x should stay at zero, as it does in C#.
Suppose we declare y as another int.
Replacing the loop body with:
y = x++;
x = y;
gives the behaviour of x staying at 0, because the assigment definitely
occurs after the increment.
Now here''s wierd:
x = y = x++;
First time through the loop, y gets 0 and x get 1. I suspect that
there''s an optimisation occuring that is making the C++ behaviour wrong.
Regards,
Jasper Kent
*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!
这篇关于c#后递增问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!