INT [] ARR = {0}; int值=编曲[ARR [0] ++];值= 1? [英] int[] arr={0}; int value = arr[arr[0]++]; Value = 1?

查看:95
本文介绍了INT [] ARR = {0}; int值=编曲[ARR [0] ++];值= 1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我来到一个交叉通过埃里克利珀文章,他试图疏通关系之谜运营商优先评估顺序。在年底,有这让我困惑的两段代码,这里是第一个片段:

Today I came a cross an article by Eric Lippert where he was trying to clear the myth between the operators precedence and the order of evaluation. At the end there were two code snippets that got me confused, here is the first snippet:

      int[] arr = {0};
      int value = arr[arr[0]++];

现在,当我想到变量值的价值,我简单地计算它是一个。以下是我认为它的工作。

Now when I think about the value of the variable value, I simply calculate it to be one. Here's how I thought it's working.


  1. 首先声明改编为int
    数组与它内部的一个项目;这种
    项的值为0。

  2. 二获得ARR [0] --0在
    这种情况。

  3. 的值
  4. 第三获取ARR(这仍然是0)[第2步的价值
    ]的值--gets
    ARR [0]再次--still 0。

  5. 第四步3
    (0)的值赋给变量的值。 --value =
    0立即

  6. 添加到第2步1 --Now
    ARR [0] = 1。
  7. $ B $的价值b
  1. First declare arr as an array of int with one item inside of it; this item's value is 0.
  2. Second get the value of arr[0] --0 in this case.
  3. Third get the value of arr[the value of step 2] (which is still 0) --gets arr[0] again --still 0.
  4. Fourth assign the value of step 3 (0) to the variable value. --value = 0 now
  5. Add to the value of step 2 1 --Now arr[0] = 1.

显然,这是错误的。我试图寻找C#的规格有关,当增量实际发生一些明确的说法,但没有发现任何。

的第二个片段是由Eric的的博客文章的话题

Apparently this is wrong. I tried to search the c# specs for some explicit statement about when the increment is actually happening, but didn't find any.
The second snippet is from a comment of Eric's blog post on the topic:

 int[] data = { 11, 22, 33 }; 
 int i = 1;
 data[i++] = data[i] + 5;

现在这里是如何,我认为这一计划将执行--AFTER声明数组并指派1到i。

Now here's how I think this program will execute --after declaring the array and assigning 1 to i. [plz bear with me]


  1. [PLZ多多包涵]获取数据[I] --1

  2. 添加到第1步中值
    5 --6

  3. 分配到数据[I](这仍然是1)
    步骤2的值--data [I] = 6
  4. 增量I - I = 2

  1. Get data[i] --1
  2. Add to the value of step 1 the value 5 --6
  3. Assign to data[i] (which is still 1) the value of step 2 --data[i] = 6
  4. Increment i -- i = 2

据我的理解,这个数组现在应该包含值{11, 27 ,33}。然而,当我循环打印数组值我得到:{11,38,33}。这意味着发布的增量提领该阵列之前发生了!

怎么来的?是不是这个职位的增量应该是帖子?即一切后发生。
我缺少什么家伙?

According to my understanding, this array now should contain the values {11, 27, 33}. However, when I looped to print the array values I got: {11, 38, 33}. This means that the post increment happened before dereferencing the array!
How come? Isn't this post increment supposed to be post? i.e. happen after everything else.
What am I missing guys?

推荐答案

的后递增操作时评价整个表达式的一部分。它的值被评估,但任何其他表达式进行求值之前,之后发生的副作用。

The postincrement operation occurs as part of evaluating the overall expression. It's a side effect which occurs after the value is evaluated but before any other expressions are evaluated.

在换句话说,对于任何表达式E,E +(如果法律)代表的东西像(伪代码):

In other words, for any expression E, E++ (if legal) represents something like (pseudo-code):

T tmp = E;
E += 1;
return tmp;

这是所有的任何事情之前评估Ë++的一部分进行评估。

That's all part of evaluating E++, before anything else is evaluated.

见的C#3.0规范的更详细的信息7.5.9。

See section 7.5.9 of the C# 3.0 spec for more details.


此外,对于其中LHS是(在此情况下)属于变量赋值操作,在LHS被评估的的RHS中进行评价。

Additionally, for assignment operations where the LHS is classified as a variable (as in this case), the LHS is evaluated before the RHS is evaluated.

因此,在你的例子:

int[] data = { 11, 22, 33 }; 
int i = 1;
data[i++] = data[i] + 5;



等同于:

is equivalent to:

int[] data = { 11, 22, 33 }; 
int i = 1;
// Work out what the LHS is going to mean...
int index = i;
i++;
// We're going to assign to data[index], i.e. data[1]. Now i=2.

// Now evaluate the RHS
int rhs = data[i] + 5; // rhs = data[2] + 5 == 38

// Now assign:
data[index] = rhs;



规范本的相关位是部分7.16.1(C#3.0规范)。

The relevant bit of the specification for this is section 7.16.1 (C# 3.0 spec).

这篇关于INT [] ARR = {0}; int值=编曲[ARR [0] ++];值= 1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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