Lifetime of a variable. : Variable Scope : Language Basics C# Examples


C# Examples » Language Basics » Variable Scope »

 

Lifetime of a variable.









    
using  System;  
  
class  Example  {  
    public  static  void  Main()  {  
        int  x;    
  
        for(x  =  0;  x  <  3;  x++)  {  
            int  y  =  -1;    
            Console.WriteLine("y  is  initialized  each  time  block  is  entered.");
            Console.WriteLine("y  is  always  -1:  "  +  y);
            y  =  100;
            Console.WriteLine("y  is  now:  "  +  y);
        }  
    }  
}
    
   
  
   



Output

y is initialized each time block is entered.
y is always -1: -1
y is now: 100
y is initialized each time block is entered.
y is always -1: -1
y is now: 100
y is initialized each time block is entered.
y is always -1: -1
y is now: 100


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Language Basics
» Variable Scope