Mega Code Archive

 
Categories / C# / Language Basics
 

Demonstrate lifetime of a variable

/* C#: The Complete Reference  by Herbert Schildt  Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Demonstrate lifetime of a variable.    using System;    public class VarInitDemo {    public static void Main() {      int x;         for(x = 0; x < 3; x++) {        int y = -1; // y is initialized each time block is entered        Console.WriteLine("y is: " + y); // this always prints -1        y = 100;         Console.WriteLine("y is now: " + y);      }    }  }