C#: VARIABLE

Variable :

Variable is an identifier which is used to identify a particular memory location. If we want to store the value in the memory (or) retrieve the value from the memory we have to use VariableName

Syntax to declare a Variable


 datatype variablename;
 

Type's Of Variable

  1. Static Variable
  2. Instance Variable
  3. Method Parameter
  4. Local Variable

1.) Static Variable

  • Static Variable must declare with static keywords
  • Static Variable must be declare with in the class and outside method with static keyword
  • 
       class A
         {
           static int x;
           void show()
            {
               // Code
            }
         }
    
     
  • The memory for the static variable will be allocated at the time of "Class Loading", whenever the class is loaded into CLR then the memory for the static variable will be allocated.
  • whenever we executed the program, the memory for the static variable will be allocated only one time at the time of loading class.
  • if the value is common for all the object then make that variable as static variable
  • the memory of static variable is allocated on "STACK" Memory
  • the scope of the static variable is through out the class (or) with in the class.
  • the lifespan of static variable is until the class is live
  • static variable will share a common memory for all the objects

2.) Instance Variable

  • Instance variable will allocated a separated memory for every object
  • Instance variable must declare inside the class and outside the method.
  • 
       class A
         {
           int x;
           void show()
            {
               // Code
            }
         }
     
     
  • Instance variable memory is allocated at the time of "Creating Object"
  • Instance variable memory is allocated in "HEAP"
  • If the values is different for the different object then we have to make that the variable Instance
  • the scope of the Instance variable is through out the class (or) with in the class.
  • the lifespan of Instance variable is until the Object is live

3.) Method Parameter

  • Method Parameter must be declare with in the method parenthesis
  • Method Parameter must be declare, if we want to pass the values for the variables at the time of calling the method then declare Method parenthesis
  • Method Parameter must be initialized at the time of Calling Method
  • the memory of Method Parameter is allocated on "STACK".
  • the memory is allocated at the time of Calling Method
  • the scope of Method Parameters is with in the Method.
  • the lifespan of Method Parameter is until the method is live

4.) Local Variable

  • the variable that was declare with in the method are called "Local Variable"
  • Local Variable must be declare with in the Block | Method
  •  
       class A
         {
           int x;
           int y;
           void Add()
            {
               int z = x+y; (here z is Local Variable )
            }
         }
     
     
  • Local Variable must be Initialized
  •  
       class A
         {
           void show()
            {
              int x;
              int y=0;
              int z;
              z=20;
         }
     
     
  • the memory is allocated for Local Variable at the time of Calling the Method
  • the memory is allocated for Local Variable on "STACK"
  • the scope of Local Variable is with in the Block | Method
  • if we want to use the variable only one time with in method | Block then declare Local Variable

Post a Comment

1 Comments