0

Something strange:

for (int i = 0; i < arr.Length; i++)           
     if (arr[i] > k)
          count++;
int  i = 0;

This throws an error:

A local parameter named 'i' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

It says that 'i' is already declared, but when I remove the int like this:

for (int i = 0; i < arr.Length; i++)           
     if (arr[i] > k)
          count++;
i = 0;

The name 'i' does not exist in the current context

3 Answers 3

4

The scope of a local variable is the whole of the block in which it is declared, including the part of the block before it.

So in your first example, the variable declared on the last line is in scope even though it can't be used (because you can't use a variable before its declaration).

You can't declare a local variable when another local variable with the same name is in scope, which is why the first snippet fails.

The second snippet fails because the scope of the variable declared in the for loop is only the for loop itself.

It might make more sense to remove loops from the picture entirely, and just use blocks. Your first example is similar to this:

// Outer block
{
    // Inner block
    {
        // Error due to the i variable declared in the outer block
        int i = 0;
    }

    // Scope of this variable is the whole of the outer block
    int i = 0;
}

Your second example is similar to this:

// Outer block
{
    // Inner block
    {
        // This declaration is fine, and the scope is the inner block
        int i = 0;
    }

    // This is invalid, because there's no variable called "i" in scope
    i = 0;
}
Sign up to request clarification or add additional context in comments.

9 Comments

WOW you have 1.14M now? You are a beast sir!
Okay, I am confused. According to sharp lab even with explicit brackets, int i is not considered inside the outer block? sharplab.io/… | I could understand it miss-infering the range of the outer block. But I asumed that with Brackets on a for, I define the outer block to end before that declaration.
And just to top it off, if I try to access i at that same spot, it tells me "out of scope"? sharplab.io/… | Is i Shroedingers variable? It is in scope and out of scope at the same time?
@Christopher: In the first of those snippets, the variable declared after the for loop is in scope for the whole block, which is why you can't declare the one in the for loop. In the second of those snippets, the variable declared inside the for loop is indeed out of scope. Both errors make sense to me. I would suggest ignoring the for loop itself, and focusing on my examples in this answer which don't have loops at all. Once you've understood those, you should then be able to apply it to the for loops (which implicitly declare a new scope).
@JonSkeet that is exactly the part that does not make sense. If I try to declare anotehr variable of the name, the variable is in scope. When I try to read or write the variable at the same point in the code, it is outside of scope. The 1st int i is both in scope and out of scope at the same point in the sourcecode.
|
1
class Program
{
    static void Main(string[] args) // START PARENT SCOPE
    {
        for( // START CHILD SCOPE
            int i = 0; // This will throw exception because i already exists in the parent scope
            i < 10; 
            i++
            )
        {
            //DO THINGS...
        } // END CHILD SCOPE

        int i = 10;
    } // END PARENT SCOPE
}

The i you defined inside the for loop, is in a child scope of the one you are working after the for loop.

Variables in scopes are considered from the end to the start of a single scope, no matter the order.

Comments

0

You declared i=0 inside the for loop. Then you re-declared it outside the loop. This is why you are having the issue.

But the scope of the variable only is for the loop itself, so you cant use it outside the loop. I hope this makes sense.

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.