儿童范围&CS0136 [英] Child Scope & CS0136

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

问题描述

以下代码无法编译,说明不能在此范围内声明名为 'st' 的局部变量,因为它会给 '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.

回复您的问题编辑.

这只是两个简单的规则:

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.

这有帮助吗?

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

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