Mega Code Archive

 
Categories / C# Tutorial / Language Basics
 

Lifetime of a variable

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