开发者

Can't use the same variablename in a method

Why can't I use 开发者_StackOverflow社区the same variablename e.g. index in a method?

Why can't the compiler see the different, when I clearly can?

Example:

private void Foo()
{
    for (int index = 0; index < 10; index++) // "first"-index
    {
         // I'm in no doubt, use "first"-index here 
         // (and only within the scope of the for loop)
    }
    int index = 0; // "second"-index
    // I'm in no doubt, use "second"-index here 
    // (and below)
}

Is it because the allocation is made at compile time? But then, why can't the compiler, under the hood, just call the "first"-index for index_1 and the "second"-index for index_2?

If I have

    private void Foo()
    {
        for (int index = 0; index < 10; index++)
        {
        }
        // the runtime don't know index here
    }

If the runtime don't know about index below the for-loop, why can't we have another variable with that name?


The declaration space of those variables overlaps even if the the scope doesn't. Check Eric Lippert's blog on that topic:

Simple names are not so simple

The purpose of all of these rules is to prevent the class of bugs in which the reader/maintainer of the code is tricked into believing they are referring to one entity with a simple name, but are in fact accidentally referring to another entity entirely. These rules are in particular designed to prevent nasty surprises when performing what ought to be safe refactorings.

What's The Difference, Part Two: Scope vs Declaration Space vs Lifetime

A declaration space, by contrast, is a region of program text in which no two entities are allowed to have the same name.

The declaration space of a variable is larger that its scope in order to prevent those misleading situations.


CodeInChaos is basically correct, and the linked article explains the rules you are violating.

You ask why the compiler cannot see the difference but you can. A strange question: obviously the compiler can see the difference. If the compiler could not work out the difference between the two meanings of "index" then how could it produce the error correctly?! The error is that there are two things that mean something different but have the same name, and so of course the compiler knows that they are different. It is precisely because the compiler knows that the two meanings of "index" are different that enables it to correctly give the error.

Moving on.

Having two local variables that mean different things is a bad practice that makes bugs and that's why there are rules that prevent it. If you really want to do this, you can but you are required to make sure that their declarations spaces do not overlap. You can do that by introducing extra braces:

{
    {
        int index; 
        // blah blah blah
    }  
    {
        int index;
        // blah blah blah
    }
}

Because now there is no space in which "index" is both declared and means two different things. (Clearly the outermost local declaration space is one in which "index" means two different things, but index is not declared in the outer declaration space.)

"for" and "foreach" loops are treated as though they have invisible braces around the whole thing, so this is legal:

{
    for(int index = 0; index <= 10; ++index) {...}
    for(int index = 0; index <= 10; ++index) {...}
}

The compiler pretends that you actually wrote

{
    {
        int index = 0; 
        while(index <= 10) { ... }
    }
    {
        int index = 0; 
        while(index <= 10) { ... }
    }
}

So again, the "invisible" braces keep the two local variable declarations textually separated.


The first index isn't defined within the scope of the for loop, that's why it cannot be re-declared outside the for loop. Inside the for loop, however, is the only place the first index can be accessed, because of how the compiler works.

Just rename the index outside the loop to index2.

To recap, both index's are in the same scope, one is just accessed in a different scope.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜