Simon Edström
Simon Edström

Reputation: 6619

How does .NET handle variables inside scope

I'm just couris about whats happning behind the scenes. I have this code and of course it will not compile cause I create the hello variable inside a if statement and later try to declare it agian. Why dosen't .NET allow me to do so? Whats happning behind the scences that could make the hello variable interfeer with the one inside the statement.

It's very stright forward why this could interfeer if the variable was declared before the if statement.

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

        Console.Write(hello);
    }

Upvotes: 1

Views: 547

Answers (3)

Eric Lippert
Eric Lippert

Reputation: 660297

Just to clarify, there are two rules violated here.

The first is that nested local variable declaration spaces may not contain two declarations of the same name.

The second is that nested local scopes may not contain two usages of the same simple name or declaration to mean 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 the simple name x is used to mean this.x in one local scope and the local variable x in a nested scope. This is confusing; a simple name is required to mean the same thing throughout its entire block. We therefore make it illegal. This would be legal:

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

Even though the local variable is declared in a scope nested inside the scope of the field, this is allowed because the field's scope is not a local variable declaration space.

This is also legal:

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

because now the two conflicting usages of x are both used consistently throughout the entirity of their immediately containing scopes. Those scopes now do not overlap, so this is allowed. (It is still a bad idea however.)

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

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.

Upvotes: 4

Corwin01
Corwin01

Reputation: 479

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

The scope of a name is the region of program text within which it is possible to refer to the entity declared by the name without qualification of the name. Scopes can be nested, and an inner scope may redeclare the meaning of a name from an outer scope. (This does not, however, remove the restriction imposed by Section 3.3 that within a nested block it is not possible to declare a local variable with the same name as a local variable in an enclosing block.) The name from the outer scope is then said to be hidden in the region of program text covered by the inner scope, and access to the outer name is only possible by qualifying the name.

Upvotes: 2

SLaks
SLaks

Reputation: 887777

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

Upvotes: 5

Related Questions