Scope class demonstrates instance and local variable scopes. : Variable Scope : Language Basics C# Source Code


Custom Search

C# Source Code » Language Basics » Variable Scope »

 

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


       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Language Basics
» Variable Scope