Can't use the same variablename in a method-Collection of common programming errors

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