method scope variable

image_pdfimage_print
   
 

using System;
class IncDecApp
{
    public static void Foo(int j)
    {
        Console.WriteLine("IncDecApp.Foo j = {0}", j);
    }
   
    static void Main(string[] args)
    {
        int i = 1;
   
        Console.WriteLine("Before Foo(i++) = {0}", i);
        Foo(i++);
        Console.WriteLine("After Foo(i++) = {0}", i);
   
        Console.WriteLine();
   
        Console.WriteLine("Before Foo(++i) = {0}", i);
        Foo(++i);
        Console.WriteLine("After Foo(++i) = {0}", i);
    }
}

    


Scope class demonstrates instance and local variable scopes.

image_pdfimage_print

   



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


          


Scoping in C#.

image_pdfimage_print
   
 


using System;

class MainClass {
    // Class level x variable
    static int x = 10;

    public static void Main() {
        // Locally defined copy of x
        int x = 5;
        int y = x;
        double z = y + 10.25;

        int a = (int)z;

        Console.WriteLine("X = {0} Y = {1} Z = {2}", x, y, z);
        Console.WriteLine("A = {0}", a);

        Console.WriteLine("Class Level X = {0}", MainClass.x);
    }
}

    


Class level Variable scope

image_pdfimage_print

   

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

namespace nsScope
{
    using System;
    public class Scope
    {
        int Var = 42;
        static public void Main ()
        {
            Scope cls = new Scope();
            int Var = 23;
            Console.WriteLine ("Class variable = " + cls.Var);
            Console.WriteLine ("Function variable = " + Var);
        }
    }
}

           
          


Variable Scoping and Definite Assignment:Definite Assignment

image_pdfimage_print

   


using System;
struct Complex
{
    public Complex(float real, float imaginary) 
    {
        this.real = real;
        this.imaginary = imaginary;
    }
    public override string ToString()
    {
        return(String.Format("({0}, {1})", real, imaginary));
    }
    
    public float    real;
    public float    imaginary;
}

public class DefiniteAssignment3
{
    public static void Main()
    {
        Complex    myNumber1;
        Complex    myNumber2;
        Complex    myNumber3;
        
        myNumber1 = new Complex();
        Console.WriteLine("Number 1: {0}", myNumber1);
        
        myNumber2 = new Complex(5.0F, 4.0F);
        Console.WriteLine("Number 2: {0}", myNumber2);
        
        myNumber3.real = 1.5F;
        myNumber3.imaginary = 15F;
        Console.WriteLine("Number 3: {0}", myNumber3);
    }
}
           
          


Definite Assignment and Arrays

image_pdfimage_print

   


using System;
struct Complex
{
    public Complex(float real, float imaginary) 
    {
        this.real = real;
        this.imaginary = imaginary;
    }
    public override string ToString()
    {
        return(String.Format("({0}, {0})", real, imaginary));
    }
    
    public float    real;
    public float    imaginary;
}

public class DefiniteAssignmentandArrays
{
    public static void Main()
    {
        Complex[]    arr = new Complex[10];
        Console.WriteLine("Element 5: {0}", arr[5]);        // legal
    }
}
           
          


declaring a reference type variable and creating an object the variable will reference

image_pdfimage_print
   

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// RefType.cs -- Demonstrate declaring a reference type variable
//               and creating an object the variable will reference.
//
//               Compile this program with the following command line:
//                   C:>csc RefType.cs
using System;
using System.IO;

namespace nsRefType
{
    public class RefType123
    {
        static public void Main ()
        {
// Declare the reference type variable
            FileStream strm;
// Create the object the variable will reference
            strm = new FileStream ("./File.txt",
                                   FileMode.OpenOrCreate,
                                   FileAccess.Write);
        }
    }
}