1

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);
    }
2

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

3 Comments

Great, something like that was what I was looking for ;-)
Just to clarify: though it is true that a name is in scope throughout the entire block which contains its declaration, there are two rules actually 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 scopes may not contain two simple names or declarations that refer to different things.
@EricLippert: I was waiting for your wisdom here, and I was not disappointed :)
4

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.

1 Comment

"It is still a bad idea however" <- understatement alert!
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.