欢迎光临
我们一直在努力

C#陷阱:int i = 10; i += i++; i = ?-.NET教程,C#语言

建站超值云服务器,限时71元/月

参加某公司的笔试时的一道题目:int i = 10; i += i++; i = ?。当时我写了21。但当我在c#中写了如下代码测试时

static void main(string[] args)

{

int i = 10;

i += i++;

console.writeline(i);

console.read();

}

编译器告诉我,结果是20。为什么!我错了吗?我开始纳闷了。我赶紧用vc(visual studio.net2003)重新编了一段测试代码,如下:

int _tmain()

{

int i = 10;

i += i++;

console::writeline(i);

console::read();

return 0;

}

这次,结果是21。奇怪了啊,为什么同样是i += i++,其结果是不一样的呢?

最终结论:语言差异

c# does have explicit rules around this behavior. (left to right)

c++ does not (problem of the c++ language, not the compiler)

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » C#陷阱:int i = 10; i += i++; i = ?-.NET教程,C#语言
分享到: 更多 (0)