Mega Code Archive

 
Categories / C# / Language Basics
 

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