Csharp/C Sharp/Language Basics/Variable Scope — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:31, 26 мая 2010

Class level Variable scope

/*
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);
        }
    }
}


method scope variable

 
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.

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#.

 

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


System maximums and minimums.

 

using System;
class MainClass {
    public static void Main() {
        // First, print out the minimum values
        Console.WriteLine("System Minimums\n");
        Console.WriteLine("MinSByte {0}", System.SByte.MinValue);
        Console.WriteLine("MinByte {0}", System.Byte.MinValue);
        Console.WriteLine("MinInt16 {0}", System.Int16.MinValue);
        Console.WriteLine("MinUInt16 {0}", System.UInt16.MinValue);
        Console.WriteLine("MinInt32 {0}", System.Int32.MinValue);
        Console.WriteLine("MinUInt32 {0}", System.UInt32.MinValue);
        Console.WriteLine("MinInt64 {0}", System.Int64.MinValue);
        Console.WriteLine("MinUInt64 {0}", System.UInt64.MinValue);
        Console.WriteLine("MinChar {0}", System.Char.MinValue);
        Console.WriteLine("MinSingle {0}", System.Single.MinValue);
        Console.WriteLine("MinDouble {0}", System.Double.MinValue);
        // Console.WriteLine( "MinBoolean {0}", System.Boolean.MinValue);
        Console.WriteLine("MinDecimal {0}", System.Decimal.MinValue);
        Console.WriteLine("\nSystem Maximums\n");
        Console.WriteLine("MaxSByte {0}", System.SByte.MaxValue);
        Console.WriteLine("MaxByte {0}", System.Byte.MaxValue);
        Console.WriteLine("MaxInt16 {0}", System.Int16.MaxValue);
        Console.WriteLine("MaxUInt16 {0}", System.UInt16.MaxValue);
        Console.WriteLine("MaxInt32 {0}", System.Int32.MaxValue);
        Console.WriteLine("MaxUInt32 {0}", System.UInt32.MaxValue);
        Console.WriteLine("MaxInt64 {0}", System.Int64.MaxValue);
        Console.WriteLine("MaxUInt64 {0}", System.UInt64.MaxValue);
        Console.WriteLine("MaxChar {0}", System.Char.MaxValue);
        Console.WriteLine("MaxSingle {0}", System.Single.MaxValue);
        Console.WriteLine("MaxDouble {0}", System.Double.MaxValue);
        Console.WriteLine("MaxDecimal {0}", System.Decimal.MaxValue);
    }
}