3

Possible Duplicate:
C# Variable Scoping

I thought I may declare two variables with the same name if in different scope:

namespace IfScope
{
    class Demo
    {
        public static void Main()
        {
            bool a = true;

            if ( a )
            {
                int i = 1;
            }
            string i  = "s";
        }
    }
}

The compiler says something else:

$ csc Var.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.

Var.cs(13,20): error CS0136: A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i', which is already used in a 'child' scope to denote something else

That would mean i declared inside the if is visible outside ( that's what I understood )

But if I try to use it then I get this.

$ cat Var.cs
namespace IfScope
{
    class Demo
    {
        public static void Main()
        {
            bool a = true;

            if ( a )
            {
                int i = 1;
            }
            i  = "s";
        }
    }
}

Var.cs(13,14): error CS0103: The name 'i' does not exist in the current context

Obviously, but what's going on here?

Community
  • 1
  • 1
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • 6
    [1]: http://stackoverflow.com/questions/2049330/c-sharp-variable-scoping – Sandeep Apr 02 '12 at 22:33
  • Short answer: A local variable is in scope in the entire block in which it is declared. That is to say that its scope includes the portion of the block before its declaration. See section 3.7 of the spec (v 4.0). The scopes are therefore not separate. – phoog Apr 03 '12 at 18:14

2 Answers2

3

C# requires that a simple name have one meaning throughout all the blocks which first uses it.From here.

From Specification.

For each occurrence of a given identifier as a simple-name in an expression or declarator, within the local variable declaration space of that occurrence, every other occurrence of the same identifier as a simple-name in an expression or declarator must refer to the same entity. This rule ensures that the meaning of a name is always the same within a given block, switch block, for-, foreach- or using-statement, or anonymous function.

Community
  • 1
  • 1
Sandeep
  • 7,156
  • 12
  • 45
  • 57
-1

The identifier i is only visible inside the if but its scope is the whole method. That means the variable that i identifies comes into existence as soon as the method starts. This is because memory has to be allocated at that time. The decision whether to allocate memory on stack cannot be made at runtime and hence all variables inside condition blocks are created before control comes into the if and the variable exists until the method returns.

wirate
  • 675
  • 1
  • 8
  • 24
  • This is not correct. You could have two separate `if` blocks which each declare the same variable. – Andrew Barber Apr 02 '12 at 22:38
  • @AndrewBarber If you declare the variable in two if statements the compiler really declares a single variable and uses it in both the scopes. Having said the at IL level the variable name is irrelevant. So in theory compiler could allow using same name if it wanted to but it would only encourage writing confusing code. – Muhammad Hasan Khan Apr 02 '12 at 22:43
  • @HasanKhan You can use the same name but different types. They really are different. Their scope is the `if`, making this answer wrong. – Andrew Barber Apr 02 '12 at 22:47
  • @AndrewBarber You are right they are different variables if types are different but the scope is really enforced by compiler. Look at the disassembled code. Scope is no magic. All locals are declared in the beginning of a method. – Muhammad Hasan Khan Apr 02 '12 at 22:50
  • but they do exist during the whole method – wirate Apr 02 '12 at 22:51
  • Yes, I understand when locals are declared. Look at the question: It is asking why the two different variables are considered to be in the same scope. This is not the correct answer to that question, as evidenced by my ability to declare two `i` variables as two different types in two different `if` blocks in the same method. – Andrew Barber Apr 02 '12 at 22:52
  • @wirate Whether they *exist* on the stack is immaterial to this question. It is asking why the *variable name* can't be used in both of those places. Variable names are not stored on the stack. – Andrew Barber Apr 02 '12 at 23:03