自转让之内后递增 [英] Post-increment within a self-assignment

查看:138
本文介绍了自转让之内后递增的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解 之间的区别我++和++我 ,但我不明白为什么我得到下面的结果:

I understand the differences between i++ and ++i, but I'm not quite sure why I'm getting the results below:

static void Main(string[] args)
{
    int c = 42;
    c = c++;
    Console.WriteLine(c);   //Output: 42
}

在上面的代码,因为这是分配变量到自身,然后递增的价值,我希望得到的结果是 43 。但是,它返回 42 。使用时,我得到了相同的结果 C = C - ; 以及

In the above code, as this is assigning the variable to itself and then incrementing the value, I would expect the result to be 43. However, it is returning 42. I get the same result when using c = c--; as well.

我知道我可以只是简单地使用 C ++; ,并用它做的,但我更好奇,为什么它的行为,它是这样的。 ?谁能解释这里发生了什么。

I realise I could just simply use c++; and be done with it, but I'm more curious why it is behaving the way that it is. Can anyone explain what's happening here?

推荐答案

让我们来看看该中介语言代码:

Let’s take a look at the intermediary language code for that:

IL_0000:  nop
IL_0001:  ldc.i4.s    2A
IL_0003:  stloc.0     // c
IL_0004:  ldloc.0     // c

这加载该常数整数 42 压入堆栈,然后将其存储到变量 C ,然后再立即将其加载到堆栈中。

This loads the constant integer 42 onto the stack, then stores it into the variable c, and loads it immediately again onto the stack.

IL_0005:  stloc.1
IL_0006:  ldloc.1

这值拷贝到另一个寄存器中,也再次加载它

This copies the value into another register, and also loads it again.

IL_0007:  ldc.i4.1
IL_0008:  add

这增加了常量1到加载值

This adds the constant 1 to the loaded value

IL_0009:  stloc.0     // c

......,结果(43)存储到变量 C

… and stores the result (43) into the variable c.

IL_000A:  ldloc.1
IL_000B:  stloc.0     // c

再从其他寄存器中的值被加载(这仍然是42!),并存储到变量 C

Then the value from the other register is loaded (that’s still 42!) and stored into the variable c.

IL_000C:  ldloc.0     // c
IL_000D:  call        System.Console.WriteLine
IL_0012:  nop
IL_0013:  ret

接着的值(42)被从可变加载,并打印出来。

Then the value (42) is loaded from the variable, and printed.

那么,你可以从这个看到的是,虽然 C ++ 加1变量的之后的返回结果,即递增发生仍是价值assinging的变量之前。所以序列更像是这样的:

So what you can see from this is that while c++ increments the variable by one after the result was returned, that incrementing happens still before assinging the value to the variable. So the sequence is more like this:


  1. 获取从 C值

  2. 后增 C

  3. 分配先前读取值到 C

  1. Get value from c
  2. Post-increment c
  3. Assign previously read value to c

和应该解释为什么你得到的结果是:)

And that should explain why you get that result :)

要添加一个例子,因为这是在评论这是因为删除提到的:

To add one more example, since this was mentioned in a comment that was since deleted:

c = c++ + c;

这个作品非常相似:再次假设2的初始值,所述加法的左侧被评估第一。因此,值从变量读取(2),然后 C 递增( C 变成3)。然后在加入的右侧进行评价。 的ç读取值(现在是3)。然后加入发生(2 + 3)和结果(5)被分配给变量

This works very similarly: Assuming an initial value of 2 again, the left side of the addition is evaluated first. So the value is read from the variable (2), then c is incremented (c becomes 3). Then the right side of the addition is evaluated. The value of c is read (now 3). Then the addition takes place (2 + 3) and the result (5) is assigned to the variable.

的外卖从这个是你应该避免在正常表达混合递增和递减操作。而行为是非常明确的,使绝对意义上(如上所示),它仍然是有时难以环绕它的头上。尤其是当你分配的东西给你的表达增加相同的变量,这成为迅速混淆。所以,做自己和别人一个忙,避免递增/递减操作的时候都没有完全靠自己:)

The takeaway from this is that you should avoid mixing increment and decrement operations in normal expressions. While the behavior is very well defined and makes absolute sense (as shown above), it is still sometimes difficult to wrap your head around it. Especially when you assign something to the same variable that you increment in the expression, this becomes confusing quickly. So do yourself and others a favor and avoid increment/decrement operations when they are not completely on their own :)

这篇关于自转让之内后递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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