为什么不是C#允许我使用相同的变量名在不同的范围?范围、变量名、不同、不是

2023-09-03 00:55:17 作者:季末不言殇

比如像:

if ( this.IsValid )
{
    Matrix matrix = new Matrix();
}

Matrix matrix = new Matrix();

编译器警告我说:

The compiler warns me saying:

命名的局部变量'基质'不能在此范围内声明的,因为它会给予不同的意义,基质',它已经用在'孩子'范围来表示别的东西。

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

未在不同的范围,这些变数,所以我不能够访问的第一个基质从if语句之外呢?

Aren't these variables in different scopes, so I wouldn't be able to access the first matrix from outside the if statement anyway?

推荐答案

到目前为止给出的答案都非常混乱。这个问题的正确分析通过的开始读取错误信息的。该错误消息告诉你什么是真正错的:

The answers given so far are very confusing. The correct analysis of the problem starts by reading the error message. The error message is telling you what is actually wrong:

名为矩阵的局部变量不能在此范围内声明的,因为这会给出一个不同的含义为矩阵,它已经用在'孩子'范围来表示别的东西。

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

阅读精心。它告诉你哪个C#的规则被侵犯precisely,即的您不能使用相同的名称来指代两种不同的东西,在相同的范围的。 (事实上​​,该错误信息是稍有不当;它应该说是局部变量声明空间的地方说范围,但毕竟是pretty的罗嗦)

Read that carefully. It is telling you precisely which rule of C# is being violated, namely that you are not allowed to use the same name to refer to two different things in the same scope. (Actually, the error message is slightly wrong; it should say "local variable declaration space" where it says "scope", but that is pretty wordy.)

这条规则被记录在C#4.0规范,部分7.6.2.1:简单的名字,不变的含义在块

This rule is documented in the C# 4.0 specification, section 7.6.2.1: Simple names, Invariant meaning in blocks.

(它的也的非法具有相同名称的重复声明空间两个局部变量,编译器可能会报告该错误为好,但它报告了更普遍的错误在这种情况下。 )

(It is also illegal to have two local variables of the same name in overlapping declaration spaces. The compiler could be reporting that error as well, but it reports the more general error in this case.)

未在不同的范围,这些变数,所以我不能够从if语句外部访问的第一个矩阵呢?

Aren't these variables in different scopes, so I wouldn't be able to access the first matrix from outside the if statement anyway?

是的。这种说法是的真正的,但是的无关的。这里的错误是的同样简单的名称已被用来指两个不同的东西,在相同的局部变量声明空间的。

Yes. That statement is true but irrelevant. The error here is that the same simple name has been used to refer to two different things in the same local variable declaration space.

考虑这种情况:

class C 
{
    int x;
    void M()
    {
        x = 10; // means "this.x"
        for(whatever)
        {
            int x = whatever;
        }
    }
 }

同样的协议。这里的误差是,简单的名称的x被用于在外部声明空间指this.x,并被用来在内部声明空间的意思是局部变量。使用相同的简单名称来指代两种不同的东西在相同的声明空间 - 记住,内声明空间是一个的部分外层的的 - 既有的混淆的和危险的,因而是非法的。

Same deal. The error here is that the simple name "x" was used in the outer declaration space to refer to this.x, and was used in the inner declaration space to mean "local variable". Using the same simple name to refer to two different things in the same declaration space -- remember, the inner declaration space is a part of the outer one -- is both confusing and dangerous, and is therefore illegal.

这是混淆的原因很明显;人有一个合理的预期,一个名字就意味着同样的事情到处都遍布在它第一次使用的声明空间。这是很危险的,因为小的code的编辑很容易改变的含义是:

It is confusing for obvious reasons; one has a reasonable expectation that a name will mean the same thing everywhere throughout the declaration space in which it is first used. It is dangerous because small code edits are prone to changing the meaning:

class C 
{
    int x;
    void M()
    {
        int x;
        x = 10; // no longer means "this.x"
        for(whatever)
        {
            x = whatever;
        }
    }
 }

如果在简单的名称首先使用的声明空间是不重叠的则是合法的简单的名称来指代不同的事情:

If the declaration spaces in which the simple names are first used are not overlapping then it is legal for the simple names to refer to different things:

class C 
{
    int x;
    void M()
    {
        {
            x = 10; // means "this.x"
        }
        for(whatever)
        {
            int x = whatever; // Legal; now the 
        }
    }
 }

有关详细信息,以及有关油炸食品一个有趣的故事,看

For more information, and an amusing story about fried food, see

http://blogs.msdn.com/b/ericlippert/archive/tags/simple+names/