Lifetime of a variable. : Variable Scope « Language Basics « C# / CSharp Tutorial






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);
    } 
  } 
}
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








1.10.Variable Scope
1.10.1.The Scope and Lifetime of Variables
1.10.2.Block scope.
1.10.3.Lifetime of a variable.