在LINQ匿名方法的局部变量的作用域(关闭) [英] local variable scope in linq anonymous method ( closure)

查看:238
本文介绍了在LINQ匿名方法的局部变量的作用域(关闭)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是LINQ查询声明的局部变量的范围内。

What is the scope of local variable declared in Linq Query.

我在写下面的code

I was writing following code

   static void Evaluate()
    {
        var listNumbers = Enumerable.Range(1, 10).Select(i => i);
        int i = 10;
    }

在线INT I = 10

编译器标记的错误,指出

Compiler flagged error on line int i=10, stating

A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i', which is already used in a 'child' scope to denote something else 

我无法理解为什么这个错误是来了。

I am unable to understand why this error is coming.

我的理解是,将第一行(在foreach循环)后,成为了范围。因此,可再次申报。

My understanding was that i will become out of scope after first line (in foreach loop). So i can be declared again.

实际的行为是不能被第一行(在foreach循环中),这是正确的后访问。但不能再次申报。这似乎很奇怪。

Actual behavior is that i cannot be accessed after first line (in foreach loop), which is correct. But i cannot be declared again. This seems strange.

编辑 这是基于响应由安德拉什一以下问题。答案是很不错的,但会导致进一步的怀疑。

EDIT This is a following question based on response by Andras. The answer is very good, but causes further doubts.

  static void Evaluate3()
    {
        var listNumbers = Enumerable.Range(1, 10).Select(i => i);
        var listNumbers1 = Enumerable.Range(1, 10).Select(i => i);
    }

根据函数的逻辑评估那个。选择(ⅰ=> i)中,并且INT I = 10,两个I,是本地的功能块,因而并发症误差

Based on the logic of function Evaluate that .Select(i=>i), and int i=10, both i, are local to function block and hence complication error.

功能Evaluate3应该不能编译,以及有两个我的方法块,但它成功地编译没有任何警告/错误。

Function Evaluate3 should not compile as well as there are two i in the method block, but it is compiling successfully without any warning/error.

问题,要么都有评估和Evaluate3不能编译,或者两者都应该编译。

Question, Either both Evaluate and Evaluate3 should not compile, or both should compile.

推荐答案

关键的事实,这里要注意的是,一个声明:

The key fact to note here is that a declaration:

int i;

...需要在整个封闭范围从生效的开始到结束的 - 不只是来自于其所宣称的点。在.NET中的局部变量的声明仅仅是一个指令编译器保留这个名字和地方的整个范围。这意味着,一旦它的宣布,它已经预留所有行的之前和之后的。

...takes effect across the whole enclosing scope from start to finish - not just from the point at which it is declared. In .Net the declaration of a local variable is just an instruction to the compiler to reserve that name and local for the entire scope. That means once it's declared, it's already reserved for all lines before and after.

在实际上,它意味着你实际上应该读评估为:

In effect, it means that you should actually read Evaluate as:

static void Evaluate()  
{  
  int i;  
  var listNumbers = Enumerable.Range(1, 10).Select(i => i);  
  i = 10;
} 

如果你编写相应的方法,你会看到发生在拉姆达声明编译器错误,而不是 - 这是完全合理的。值得庆幸的是,C#编译器是足够聪明,从人性的角度,认识到code的顺序是非常重要的的我们的,它实际上赋予了编译器错误到任何电源线是在第二或随后的声明;因此为什么在你的版本评估碰巧就行了 INT I = 10; 。随着函数的局部的实际寿命这方面的知识我,编译器是正确的:使用有< STRONG>的的与拉姆达较早使用我冲突。

And if you do write your method accordingly, you'll see that the the compiler error occurs on the lambda declaration instead - which is perfectly reasonable. Thankfully, the C# compiler is clever enough, from a human perspective, to recognise that ordering of code is important to us, and it actually assigns the compiler error to whichever source line is the second or subsequent declaration; hence why in your version of Evaluate it happens on the line int i = 10;. With this knowledge of the actual lifetime of the function's local i, the compiler is correct: the use of i there will conflict with the earlier use of i in the lambda.

您可以用明确的范围界定,以避免这种情况:

You can use explicit scoping to avoid this:

static void Evaluate()  
{
  var listNumbers = Enumerable.Range(1, 10).Select(i => i);
  {
    int i = 10;
  }
}

Evaluate3 您只需注意的是,尽管这两个lambda表达式共享父功能范围,他们也有他们自己的,而且它在那里,他们的<$ C $的情况下, C>我 s的声明 - 这就是为什么他们不互相干扰(他们是,实际上,同级范围)

In the case of Evaluate3 you simply note that whilst both lambdas share the parent function scope, they also have their own, and it's in there that their is are declared - and that's why they don't interfere with each other (they are, in effect, sibling scopes).

顺便说一句评估 Evaluate3 最终可以简化为这样的:

Incidentally Evaluate and Evaluate3 can ultimately be simplified to this:

static void Evaluate()
{
  { 
    int i;
  }
  int i; //<-- error
}

static void Evaluate3()
{
   { 
     int i;
   }
   { 
     int i;
   }
   //fine here - both i's are in different scopes.
}

和它实际上是第二个在这里的情况,我已经用明确的范围界定为前 - 相同的功能,其中实际上有一个内,在不同的范围不同类型的每个。就像我说的 - 我从来没有再做它和code的问题已经不再是活的:)

And it's actually the second scenario here that I've used explicit scoping for before - that is, in different scopes within the same function, where the i actually has a different type in each. Like I say - I've never done it again and the code in question is no longer live :)

这篇关于在LINQ匿名方法的局部变量的作用域(关闭)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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