Lambda范围澄清 [英] Lambda Scope Clarification

查看:103
本文介绍了Lambda范围澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的参数 x 表现得如此不规律?

Why does my parameter x behave so erratically?


  1. 示例1-在当前上下文中不存在。

  2. 示例2-无法重用 x ,因为它是在子范围内定义的。 / li>
  3. 示例3-很好。这是我感到困惑的部分。也许其他的孩子范围是什么?

  1. Example 1 - Doesn't exist in the current context.
  2. Example 2 - Cannot reuse x because it's defined in a 'child' scope.
  3. Example 3 - Fine. This is the part where I am confused. Perhaps a different 'child' scope?

示例1

List<int> list = new List<int> { 1, 2, 3, 4, 5 };
var result = list.Where(x => x < 3);
Console.Write(result.ElementAt(x));

创建此编译时错误:


x在当前上下文中不存在

The name 'x' does not exist in the current context

我希望如此。

示例2

List<int> list = new List<int> { 1, 2, 3, 4, 5 };
var result = list.Where(x => x < 3);
int x = 1;
Console.Write(result.ElementAt(x));

会产生此编译时错误:


在此范围内不能声明名为'x'的局部变量,因为它的
赋予'x'不同的含义,而x已在
'child'中使用。表示其他内容的范围

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

我了解此问题的答案, 。但是,这是我从未见过的东西。此外,它还会回答以下问题: 是不完整的还是错误的?

I understand the scoping as answered in this question, Is there a reason for C#'s reuse of the variable in a foreach?. However, this is something I've never seen before. In addition, it makes the answers to this question, What is the scope of a lambda variable in C#?, incomplete or wrong.

示例3

List<int> list = new List<int> { 1, 2, 3, 4, 5 };
List<string> stringList = new List<string> { "A", "B" };
var result = list.Where(x => x < 3);
var result2 = stringList.Where(x => x != "A");

Console.Write(result2);

没有错误产生。



有了埃里克·利珀特(Eric Lippert)的这些博客帖子,答案得到了接受,这使我对正在发生的事情wrap之以鼻。如果仍然有人困惑:


With the accepted answer, these blog posts from Eric Lippert helped me wrap my head around what was happening. If anyone is still confused:

声明空间

简单名称

推荐答案

示例1 ,x在lamdba表达式的局部范围内定义,第三行不可见

In Example 1, x is defined in the local scope of the lamdba expression and is not visible to the third line

示例2 ,现在您已经在相同的声明范围内声明了两个名为 x的变量(可见性不同)

In Example 2, now you've declared two variables named "x" at the same declaration scope (visibility is different)

带有lambda或匿名方法,它捕获了运行范围。如果您的本地x与lambda定义的作用域相同,则它将捕获该x以拉入lambda可以访问的内容-从而产生两个 x的定义。您在lambda中声明的内容不会在另一个方向被捕获,因此在lambda之外不可见。

With a lambda or anonymous method, it "captures" the scope at which it is running. If you have a local x in the same scope as the lambda definition, then it "captures" that x to pull into what the lambda can access--thus resulting in two definitions of "x". What you declare in the lambda doesn't get captured in the other direction so it isn't visible outside the lambda.

示例3 ,现在您将不使用仅在lambda之外的lambda局部变量,并且不在相同的声明范围内命名相同的变量。

In Example 3, Now you're not using a variable that is local only the lambda outside the lambda, and not naming something the same at the same declaration scope.

这篇关于Lambda范围澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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