对于i = 0,这是为什么(我+ =我++)等于0?这是

由网友(长相思莫相忘)分享简介:看看下面code(可作为一个控制台应用程序):Take the following code (usable as a Console Application):static void Main(string[] args){int i = 0;i += i++;Console.WriteLine(i);Cons...

看看下面code(可作为一个控制台应用程序):

Take the following code (usable as a Console Application):

static void Main(string[] args)
{
    int i = 0;
    i += i++;
    Console.WriteLine(i);
    Console.ReadLine();
}

的结果,我 0。我预计2(我的一些同事一样)。也许是编译器创建某种结构,导致在为零。

The result of i is 0. I expected 2 (as some of my colleagues did). Probably the compiler creates some sort of structure that results in i being zero.

我预计2的原因是,在我的思路,右手声明将首先评估,我递增以1比它加入到我。既然我已经是1,则加1比1。所以,1 + 1 = 2。显然,这不是发生了什么。

The reason I expected 2 is that, in my line of thought, the right hand statement would be evaluated first, incrementing i with 1. Than it is added to i. Since i is already 1, it is adding 1 to 1. So 1 + 1 = 2. Obviously this is not what's happening.

您可以解释一下什么是编译器或运行时会发生什么?为什么结果为零?

Can you explain what the compiler does or what happens at runtime? Why is the result zero?

部分-排序的-免责声明:我绝对知道你不会(或许不应该)使用code。我知道我永远也不会。不过,我觉得很有趣的是,知道为什么它的行为以这样的方式和正在发生的事情完全是。的

推荐答案

本:

int i = 0;
i += i++

可以看到,你做什么(以下是过于简单化):

Can be seen as you doing (the following is a gross oversimplification):

int i = 0;
i = i + i; // i=0 because the ++ is a postfix operator and hasn't been executed
i + 1; // Note that you are discarding the calculation result

而实际上,更多地参与比 - 看看MSDN, 7.5.9后缀增量和减量运算符的:

形式的后缀增量或减量运算的运行时处理X ++或x--包括以下步骤:

The run-time processing of a postfix increment or decrement operation of the form x++ or x-- consists of the following steps:   

如果x属于一个变量: 如图,v取多大值时,电流I不等于零 若v从0开始增加,UD如何变化 电流I如何变化 使估算电流I

If x is classified as a variable:   在计算x以产生变量。    x的值被保存。   在所选择的操作符被调用x的保存值作为参数。   由操作者返回的值存储在由x的计算给定的位置。    x的存储的数据变为操作的结果。    x is evaluated to produce the variable. The value of x is saved. The selected operator is invoked with the saved value of x as its argument. The value returned by the operator is stored in the location given by the evaluation of x. The saved value of x becomes the result of the operation.

请注意,由于秩序,后缀 + 出现的在的 + = ,但结果呢?不用的(如previous值时)。

Note that due to order of precedence, the postfix ++ occurs before +=, but the result ends up being unused (as the previous value of i is used).

的更彻底分解I + = I + 来它是由需要一个知道的部分,这两个 + = + 不是原子(即,没有一个是单人操作),即使它们看起来像他们。这些实施方式包括临时变量,副本的操作发生前 - 每个操作。 (我将用名称 IADD iAssign 对用于 + 和 + = 分别)。

A more thorough decomposition of i += i++ to the parts it is made of requires one to know that both += and ++ are not atomic (that is, neither one is a single operation), even if they look like they are. The way these are implemented involve temporary variables, copies of i before the operations take place - one for each operation. (I will use the names iAdd and iAssign for the temporary variables used for ++ and += respectively).

所以,更接近于正在发生的事情是:

So, a closer approximation to what is happening would be:

int i = 0;
int iAdd = i; // Copy of the current value of i, for ++
int iAssign = i; // Copy of the current value of i, for +=

i = i + 1; // i++ - Happens before += due to order of precedence
i = iAdd + iAssign;