Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C# - SCOPE


Scope can be defined as the accessibility of a variable.


Say for example, in every office, there is a restricted area where only a few people have access.


Similarly, in C#, the variables also have some access restrictions. The variables that are defined inside a Method, cannot be accessed outside of it.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        myMethod();
        System.Console.WriteLine(x);
    }
    
    static void myMethod() 
    {
	    int x = 5;	
    }
}


Output :



  error CS0103: The name 'x' does not exist in the current context

So, in the first line itself, the Method myMethod() is called.


myMethod();

And the function execution begins.


static void myMethod()
{
	int x = 5;
}

And initialised the variable x with 5 inside the function fun().


int x = 5;
C_Sharp


Then we come to the print statement,


System.Console.WriteLine(x);

And end up with an error as output,

Output :



  error CS0103: The name 'x' does not exist in the current context

This is because the variable x is not accessible outside the Method. And that is called as Local Scope.


Local Scope


The variables that are declared inside a Method, can only be accessed inside the Method and is local to that Method. That is called the Local Scope of a variable.


Say for example, if there are two functions, firstMethod() and secondMethod().


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        firstMethod();
	    secondMethod();
    }
    
    static void firstMethod() 
    {
	    int x = 5;
	    System.Console.WriteLine("The variable x : "+x+" is not accesible outside the firstMethod()");
    }	
	
    static void secondMethod() 
    {
	    int y = 7;
	    System.Console.WriteLine("The variable y : "+y+" is not accesible outside the secondMethod()");
    }
}	


Output :



  The variable x : 5 is not accesible outside the firstMethod()
  The variable y : 7 is not accesible outside the secondMethod()

So, we have declared two functions, firstMethod(),


static void firstMethod()
{
	int x = 5;
	System.Console.WriteLine("The variable x : "+x+" is not accesible outside the firstMethod()");
}

And secondMethod().


static void secondMethod()
{
	int y = 7;
	System.Console.WriteLine("The variable y : "+y+" is not accesible outside the secondMethod()");
}

And we have two variables, x and y.


While x is declared inside the Method firstMethod() and is local to the firstMethod() and cannot be accessed outside it.


Similarly, y is declared inside the Method secondMethod() and is local to the secondMethod() and cannot be accessed outside it.


Next, let us see the Local Scope in nested Method.


Local Scope in Nested Method


Nested Method is a Method inside an existing Method.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        outerMethod();
    }
    
    static void outerMethod() 
    {
        int x = 5;
        System.Console.WriteLine("The variable x is accesible from the outerMethod() and the value is "+ x);

        void innerMethod() 
        {
            System.Console.WriteLine("The variable x is accesible from the innerMethod() and the value is "+ x);
        }
    	innerMethod();
    }   
}


Output :



  The variable x is accesible from the outer_func() and the value is 5
  The variable x is accesible from the inner_func() and the value is 5

So, in the above example, there are two Methods, outerMethod() and innerMethod(). And the innerMethod() resides inside the outerMethod().


Now, we have declared a variable x inside the outerMethod().


static void outerMethod()
{
	int x = 5;
	System.Console.WriteLine("The variable x is accesible from the outerMethod() and the value is "+ x);

	void innerMethod()
	{
		System.Console.WriteLine("The variable x is accesible from the innerMethod() and the value is "+ x);
	}
	innerMethod();
}

Now, since the innerMethod() resides inside the outerMethod(). The variable x is also accessible inside the innerMethod().


Now, what if, the variable x was defined inside the innerMethod(). Can the variable x be accessed from the outerMethod()?


Well! The answer is no.


Let us see the next example.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        outerMethod();
    }
    
    static void outerMethod() 
    {
        System.Console.WriteLine("The variable x is accesible from the outerMethod() and the value is "+ x);

        void innerMethod() 
        {
            int x = 5;
            System.Console.WriteLine("The variable x is accesible from the innerMethod() and the value is "+ x);
        }
        innerMethod();
    }   
}


Output :



  error CS0103: The name 'x' does not exist in the current context

So, in the above code, we have declared the variable x inside the inner_func().


static void outerMethod()
{
	System.Console.WriteLine("The variable x is accesible from the outerMethod() and the value is "+ x);

	void innerMethod()
	{
		int x = 5;
		System.Console.WriteLine("The variable x is accesible from the innerMethod() and the value is "+ x);
	}
	innerMethod();
}

So, the variable x is not accesible from the outerMethod().


So, the moral of the story is, the variable that is declared at a higher scope could be accessed at a lower scope.


Say for example, your Boss will have access to your details. But you will not have access to any of your Boss's details.