Block scope. : Variable Scope « Language Basics « C# / CSharp Tutorial






using System; 
 
class Example { 
  public static void Main() { 
    int x; // known to all code within Main() 
 
    x = 10; 
    if(x == 10) { // start new scope
      int y = 20; // known only to this block 
 
      // x and y both known here. 
      Console.WriteLine("x and y: " + x + " " + y); 
    } 
    // y = 100; // Error! y not known here  
 
    // x is still known here. 
    Console.WriteLine("x is " + x); 
  } 
}
x and y: 10 20
x is 10








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.