Scope of Variables in C#

Scope of Variable is the region of code within which the variable can be accessed. It depends on the type of the variable and place of its declaration. In C#, there are several types of variables:

  • Static Variables
  • Instance Variables
  • Local Variables
  • Array Elements
  • Value Parameters
  • Reference Parameters
  • Output Parameters
  • class Test
    {
      static int p;
      int n;
      void display(int x, ref int y, out int z, int [] a)
    {
      int q=25;
      ...... 
      ......
    }
    }
    

  • p as a static variable
  • n as an instance variable
  • x as a value parameter
  • y as a reference parameter
  • z as an output parameter
  • a[0] as an array element
  • q as a local variable