INT X = 10; X + = x--;在.net中 - 为什么? [英] int x = 10; x += x--; in .Net - Why?

查看:172
本文介绍了INT X = 10; X + = x--;在.net中 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int x = 10;
x += x--;

在C#/。NET,为什么它等于它等于? (我故意留下的答案,所以你可以猜测,看看你是对的)

In C#/.Net, why does it equal what it equals? (I'm purposely leaving the answer out so you can guess and see if you're right)

推荐答案

看看这个语句:

x += x--;

这是等价于:

x = x + x--;

这是等价于:

Which is equivalent to:

int a1 = x; // a1 = 10, x = 10
int a2 = x--; // a2 = 10, x = 9
x = a1 + a2; // x = 20

所以 X 是20后 - 而这保证了规范

So x is 20 afterwards - and that's guaranteed by the spec.

什么的的pretty的很多保证,但不是由规范,就是使用这种code的人会被他们的同事的攻击。是的,这是很好的结果是predictable。不,这不是很好的使用的那种code。

What's also pretty much guaranteed, although not by the spec, is that anyone using such code will be attacked by their colleagues. Yes, it's good that the result is predictable. No, it's not good to use that kind of code.

这篇关于INT X = 10; X + = x--;在.net中 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆