Identify the scope of the variables within a method in C#

Today we will discuss the scope of the variables within a method. We know that in c# we can declare local variables in any block, block refers the code between opening and closing curly braces or we can say that a block is begun with a opening curly brace and ended by a closing curly brace. See the following:

// code
{
// block's body
}

Consider another code of lines:

if(condition)
{
// write your code
}

Now we talk about the scope of variables. Suppose in c# method we declare a variable just after opening curly brace, consider the following example:
 

static void Main(string[] args)
    {
        // block1 start
        int i = 6;
        if (i > 5)
        {
            // block2 start
            Console.WriteLine("The value of i is greater than Five");
            // write other code
            //block2 end
        }
        else
        {
            // block3 start
            Console.WriteLine("The value of i is less than Five");
            // write other code
            //block3 end
        }
        //block1 end
    }

 
The above program has the three block or scope area. I have commented the lines at the beginning of the new scope and at the end of the scope.
The first scope begins with the declaration of the type int i so i can accessible in the anywhere of the method. You can use i in the if and else body.
And now consider the following:

    static void Main(string[] args)
    {
        // block1 start
        int i = 6;
        if (i > 5)
        {
            int y = 0;
            y = i;
            // block2 start
            Console.WriteLine("The value of i is greter than Five");
            // write other code
            //block2 end
        }
        else
        {
            // y = i; This statement will give the error as: The name 'y' does not exist in the current context
            // block3 start
            i = 6;
            Console.WriteLine("The value of i is less than Five");
            // write other code
            //block3 end
        }
        //block1 end
    }

The variable i is declared at the start of the main()’s scope and is accessible to all subsequent code with in Main(). With in if block, y is declared, y is visible only within its block (see the block2 in comment) and within if block i can be used because if block is the in side the block of y.
 
One Another thing we can access the variable before its declaration. The variable can be available after the its declaration.