Scope class demonstrates instance and local variable scopes.
using System;
public class Scope
{
private int x = 1;
public void Begin()
{
int x = 5;
Console.WriteLine(x );
UseLocalVariable();
UseInstanceVariable();
UseLocalVariable();
UseInstanceVariable();
Console.WriteLine(x );
}
public void UseLocalVariable()
{
int x = 25;
Console.WriteLine("UseLocalVariable is {0}", x );
x++;
Console.WriteLine("before exiting UseLocalVariable is {0}", x );
}
public void UseInstanceVariable()
{
Console.WriteLine( "instance variable x on entering {0} is {1}","method UseInstanceVariable", x );
x *= 10;
Console.WriteLine( "instance variable x before exiting {0} is {1}","method UseInstanceVariable", x );
}
}
public class ScopeTest
{
public static void Main( string[] args )
{
Scope testScope = new Scope();
testScope.Begin();
}
}
Related examples in the same category