Demonstrate block scope : Variable Definition : Language Basics C# Source Code


Custom Search

C# Source Code » Language Basics » Variable Definition »

 

Demonstrate block scope









    

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate block scope. 
 
using System; 
 
public class ScopeDemo { 
  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); 
      x = y * 2; 
    } 
    // y = 100; // Error! y not known here  
 
    // x is still known here. 
    Console.WriteLine("x is " + x); 
  } 
}


           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Language Basics
» Variable Definition