儿童范围和放大器; CS0136 [英] Child Scope & CS0136

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

问题描述

下面的代码无法编译,指出名为'ST'的局部变量不能在此范围内声明,因为它会给予不同的意义,这已经​​是一个'孩子'范围用来表示别的东西'ST',

The following code fails to compile stating "A local variable named 'st' cannot be declared in this scope because it would give a different meaning to 'st', which is already used in a 'child' scope to denote something else":

        var l = new List<string>();
        l.Find(st => st.EndsWith("12"));
        string st = "why this fails?";



我明白为什么这是行不通的:

I understand why this won't work:

        string preParent = "";
        {
            string preParent = "Should fail cause we change the meaning";
        }

当我们这样做,下面我们得到CS0103:名称'postParent'呢在目前情况下不存在的:

When we do the following we get "CS0103: The name 'postParent' does not exist in the current context":

        {
            string postParent=string.Empty;
        }
        postParent = "Should this work?";



我不明白的是为什么编译器足够聪明地看到,postParent不在范围内,但不会让我定义具有相同的名称作为一个孩子的范围(这显然是超出范围在这一点上)中使用的变量的新变量。

What I don't get is why is the compiler smart enough to see that postParent is not within scope, but won't let me define a new variable that has the same name as a variable used within a child scope (which is obviously out of scope at this point).

时的拒绝让我使用变量编译器强制执行简单的适用范围?如果是这样,这是有道理

Is the compiler simple enforcing scope by refusing to let me use the variable? If so this makes sense.

===========

===========

编辑:

我想我也觉得有趣的是你可以在一个单一的方法两个子范围内的同一个变量,所以这是有效的:

I guess what I also find interesting is how you can have the same variable within two child scopes in a single method, so this is valid:

        {
            string thisWorks= string.Empty;
        }
        {
            string thisWorks= "Should this work?";
        }



我只是有点好奇,你可以有两个变量具有相同只要它们是在同一级别名称(如果你看看范围为一棵树)。这是有道理的,因为你可以在具有相同名称相同类的两个方法有局部变量。

I'm just a little curious that you can have two variables with the same name as long as they are at the same level (if you look at scope as a tree). This makes sense because you can have local variables in two methods of the same class with the same name.

我只是惊讶,编译器能够区分并允许这样做,虽然它不会允许postParent变量。这是一个技术上的限制或者这是一个设计决策?这就是我真正想要知道的; - )

I'm just surprised that the compiler is able to differentiate and allow this, while it wouldn't allow the postParent variable. And is this a technical limitation or was this a design decision? That's what I'm really trying to get at;-)

推荐答案

是的,编译器执行范围。需要注意的是一个变量的作用域是词法块它的一部分 - 不只是从申报开始点,而是整个范围。

Yes, the compiler is enforcing scope. Note that the scope of a variable is the lexical block it's part of - not just from the point of declaration onwards, but the whole scope.

编译器是抱怨,因为分配给 postParent 是它的范围之外(这仅是嵌套的括号)。如果你试图在您目前正在分配给 postParent 问题将与嵌套块点声明一个新的变量,因为范围 postParent 将包括嵌套块,即使它是在声明之前。

The compiler is complaining because the assignment to postParent is outside its scope (which is only the nested braces). If you tried to declare a new variable at the point where you're currently assigning to postParent the problem would be with the nested block, because the scope of postParent would include that nested block, even though it was before the declaration.

作用域在C#3.0规范的3.7节所述。

Scopes are described in section 3.7 of the C# 3.0 specification.

编辑:为了回应你的问题修改

To respond to your question edit.

这只是两个简单的规则:

It's just two simple rules:


  • 您不能声明一个局部变量时另一个同名的局部变量范围

  • 的范围局部变量是该声明所在

我敢肯定的语言可能被设计成的范围才开始在声明的观点,但我认为这是简单(在语言复杂性方面)考虑范围,有只有几个街区 - 所以在同一个块中声明的所有局部变量具有相同的范围,例如。这让生活变得简单许多考虑捕获变量时,太 - 因为什么被抓获取决于范围,嵌套的作用域让生活有趣...

I'm sure the language could have been designed such that the scope only began at the point of declaration, but I think it's simpler (in terms of language complexity) to consider scopes as just blocks - so all local variables declared in the same block have the same scope, for example. That makes life a lot simpler when considering captured variables, too - as what gets captured depends on the scope, and nested scopes make life interesting...

编辑:语言规格有这样说原来的lambda表达式的例子 - 这是一节7.14.1:

The language spec has this to say about the original lambda expression example - it's section 7.14.1:

可选的
匿名功能 - 一个
匿名函数签名定义名称
和可选的类型正式
参数的匿名函数。

匿名函数的参数的范围是
匿名函数体。连同
参数列表(如果有),将
匿名方法体构成
声明空间。出于这个原因,它
为匿名
函数的参数的名称
键匹配的本地
变量,局部恒定,或名称的编译时错误参数
,其范围包括
匿名方法表达或
的lambda表达式。

The optional anonymous-function-signature of an anonymous function defines the names and optionally the types of the formal parameters for the anonymous function. The scope of the parameters of the anonymous function is the anonymous-function-body. Together with the parameter list (if given), the anonymous-method-body constitutes a declaration space. For this reason, it is a compile-time error for the name of a parameter of the anonymous function to match the name of a local variable, local constant, or parameter whose scope includes the anonymous-method-expression or lambda-expression.

这是否帮助?

这篇关于儿童范围和放大器; CS0136的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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