Scope variable inside if
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/2049330/c-sharp-variable-scoping">C# Variable Scoping</a>  </p>
</blockquote>



<p>I thought I may declare two variables with the same name if in different scope: </p>

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

            if ( a )
            {
                int i = 1;
            }
            string i  = "s";
        }
    }
}
</code></pre>

<p>The compiler says something else: </p>

<pre><code>$ 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
</code></pre>

<p>That would mean <code>i</code> declared inside the if is visible outside ( that's what I understood ) </p>

<p>But if I try to use it then I get this.</p>

<pre><code>$ 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
</code></pre>

<p>Obviously, but what's going on here?</p>
