By 22/07/2015 7 Comments

C# Variable Scopes

  • The scope of a variable is an area or region of code which indicates that where these variables is being accessed from that area or region.

┬áThere are 3 types of variable’s scope:

  • Class level scope.
  • Method level scope.
  • for, while and do while loop, if and switch statements scope.

Class level scope:

  • When we create a class and declare a variable in that class, this is called class level scope because that variable is being declared in the class scope.
  • Class level scope variables can be accessed in all methods which is declared in the class.
  • The variables which are declared in class or in class level scope are called fields or class member variables.

Example:

    class Test
    {  // start class scope

      // initialize a variable or a class field in class scope
        string colorName = "red";

      // declare a method Get() of type void 
        public void GetValue()            
        {
          /* access class field or variable in method, this colorName field is 
             accessible here because it is being initialized in class scope and 
             it is available in this method until a class scope ends */
            Console.WriteLine(colorName); 
        }   // End method definition

    } // End class scope

Method level scope:

  • When we create a method in a class, declare or initialize a variable inside that method means we are creating a method level scope.
  • Variables are being declared in any method are called local variables.

Example:

 class Program
 {      // start class scope
        public static void ShowVariable1()
        {  // start first method scope

            //initialize an int variable (number) with value 100.
            int number = 100;

            // Print number variable.
            Console.WriteLine(number);

        }  // End ShowVariable1 ()'s scope

        public static void ShowVariable2()	   // start second method scope
        {
          // Try to access same local variable (number) which is already declared in above ShowVariable1 () method. compile-error will be occurred.
            Console.WriteLine(number);                    

        } // End ShowVariable2 ()'s scope
    }

Remember:

  • You cannot declare local variables twice with the same name in same scope. If you do that you will face a compile time error.

for, while and do while loop, if and switch statements scope:

  • Variables which are being declared inside for, while, if and switch statement’s body are called local variables.
  • These variables define their scope inside the body of these statements, so this is called statements scope.

Example:

class Program
    {  // start class scope
        static void Main(string[] args)         // start main method scope
        {
            for (int i = 0; i <= 10; i++)
            {  // start for loop scope.

                // Access that local i variable inside for loop's body.
                Console.WriteLine(i);
            }   // End for loop scope

            // Try to access same local variable (i) which has  already declared in above for loop statement. A compile-time error will be occurred.
            Console.WriteLine(i);
            Console.ReadKey();
        }   // End main method scope
    }   // End class scope

Remember:

  • Above example is being associated with only for loop, this is not limited, we can use variable’s statements scope with all available statements in C# such as if, switch, do while and while loop statements.






← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .