Why can't I declare a variable with the same name in different scopes in C#? [duplicate]

只愿长相守 提交于 2021-02-16 13:53:49

问题


Not sure if this is because the C# compiler is extra picky, but I try to do this in C#:

public static void Main()
{
    bool result = true; // some dummy value
    if(result)
    {
        int x = 5;
        Console.WriteLine(x);
    }

    int x = 10;
    Console.WriteLine(x);
}

The compiler complains that the variable name "x" is already being used:

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.

And I understand that it thinks it's a scope issue, but why does it think that?

If I reproduce the same code in Java, there are no problems.


回答1:


Not every {} starts a new scope. The integer declared in the if block is still on the same stack as the function.




回答2:


From MSDN Compiler Error CS0136

For each occurrence of a given identifier as a simple-name in an expression or declarator, within the local variable declaration space (§3.3) immediately enclosing that occurrence, every other occurrence of the same identifier as a simple-name in an expression or declarator must refer to the same entity. This rule ensures that the meaning of a name is always the same within a given block, switch block, for-, foreach- or using-statement, or anonymous function.

As a second reference, check Variable scope confusion in C# answers which you can find good information in.

You are allowed to use the same variable name in non-overlapping scopes. If one scope overlaps another, though, you cannot have the same variable declared in both. The reason for that is to prevent you from accidentally using an already-used variable name in an inner scope




回答3:


As @BlackFrog correctly points out, each { } does not start a new scope. From the C# Language Specification, §3.3:

• Each method declaration, indexer declaration, operator declaration, instance constructor declaration and anonymous function creates a new declaration space called a local variable declaration space. Names are introduced into this declaration space through formal parameters (fixed-parameters and parameter-arrays) and type-parameters. The body of the function member or anonymous function, if any, is considered to be nested within the local variable declaration space. It is an error for a local variable declaration space and a nested local variable declaration space to contain elements with the same name.

(emphasis mine)




回答4:


I think it's more obvious why if you move the outer declaration of x above the if statement.

public static void Main()
{
    bool result = true; // some dummy value

    int x = 10;

    if(result)
    {
        //references x in parent scope
        x = 5;
        //x is already defined, can't define again
        int x = 5;
        Console.WriteLine(x);
    }

    Console.WriteLine(x);
}

Clearly the compiler would see x as being a variable which is already defined inside the if statement when the code tries to declare it again.



来源:https://stackoverflow.com/questions/35376039/why-cant-i-declare-a-variable-with-the-same-name-in-different-scopes-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!