How .NET handles variables within scope

I just listen to what is happening behind the scenes. I have this code and of course it won't compile because I create a hello variable inside an if statement and then try to declare it agian. Why does Dosen't.NET allow me to do this? What's going on outside of the scope that a hello variable interfeer could do with what's inside the statement.

It is very difficult to understand why this might be due to the fact that the variable was declared before the if statement.

    public void Test() {
        if (true)
        {
            var hello = "";
        }
        var hello = "";

        Console.Write(hello);
    }

      

+1


source to share


3 answers


To clarify, two rules are violated here.

First, nested local variable declaration spaces may not contain two declarations with the same name.

Second, nested local scopes may not contain two usages with the same simple name or declaration meaning two different things.

Both rules are violated. Note that the first rule is essentially a special case of the second rule. The second rule is more general; for example, this is also a violation:

class C
{
    int x;
    void M()
    {
        if (whatever)
        {
            string x = "";
        }
        x = 10;
    }
}

      

Here, a simple name is x

used to refer this.x

to one local scope and a local variable x

to a nested scope. This confuses; a simple name must mean the same throughout its block. Therefore, we make it illegal. This would be legal:

class C
{
    int x;
    void M()
    {
        if (whatever)
        {
            string x = "";
        }
    }
}

      

Although a local variable is declared in scope nested within the scope of the field, this is permitted because the scope of the field is not the scope of the local variable declaration.



It is also legal:

class C
{
    int x;
    void M()
    {
        if (whatever)
        {
            string x = "";
        }
        else
        {
            x = 2;
        }
    }
}

      

because now the two conflicting uses x

are used consistently in their entirety immediately containing areas. Now these areas do not overlap, so this is allowed. (This is still a bad idea, however.)

These rules are there for your safety, but they can be quite confusing. Cm

http://blogs.msdn.com/b/ericlippert/archive/tags/declaration+spaces/

and

http://blogs.msdn.com/b/ericlippert/archive/tags/scope/

for some articles on these language features.

+4


source


The name declaration space expands to fill its entire block, even before the declaration.
See Eric Lippert's blog post .



+5


source


From http://msdn.microsoft.com/en-us/library/aa691132(v=vs.71).aspx :

A namespace is an area of โ€‹โ€‹program text within which you can refer to an object declared as a name without name qualification. Scopes can be nested, and the inner scopes can override the name value from the outer scopes. ( This does not, however, remove the restriction imposed by Section 3.3 that it is not possible to declare a local variable in a nested block with the same name as a local variable in a closing block. ) the name from the outer scope is then considered hidden in the area of โ€‹โ€‹the program text covered by the inner scope, and the external name can only be accessed by qualifying the name.

+2


source







All Articles