Demonstrate block scope


   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate block scope. 
 
using System; 
 
public class ScopeDemo { 
  public static void Main() { 
    int x; // known to all code within Main() 
 
    x = 10; 
    if(x == 10) { // start new scope
      int y = 20; // known only to this block 
 
      // x and y both known here. 
      Console.WriteLine("x and y: " + x + " " + y); 
      x = y * 2; 
    } 
    // y = 100; // Error! y not known here  
 
    // x is still known here. 
    Console.WriteLine("x is " + x); 
  } 
}


           
          


Demonstrate dynamic initialization


   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate dynamic initialization.  
  
using System;  
  
public class DynInit {  
  public static void Main() {  
    double s1 = 4.0, s2 = 5.0; // length of sides 
  
    // dynamically initialize hypot  
    double hypot = Math.Sqrt( (s1 * s1) + (s2 * s2) ); 
  
    Console.Write("Hypotenuse of triangle with sides " + 
                  s1 + " by " + s2 + " is "); 
 
    Console.WriteLine("{0:#.###}.", hypot); 
 
  }  
}


           
          


Int, float, double, decimal


   

/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/
 using System;
 public class IntFloatDoubleDecValues
 {
     static void Main()
     {
         int firstInt, secondInt;
         float firstFloat, secondFloat;
         double firstDouble, secondDouble;
         decimal firstDecimal, secondDecimal;

         firstInt = 17;
         secondInt = 4;
         firstFloat = 17;
         secondFloat = 4;
         firstDouble = 17;
         secondDouble = 4;
         firstDecimal = 17;
         secondDecimal = 4;
         Console.WriteLine("Integer:	{0}
float:		{1}",
             firstInt/secondInt, firstFloat/secondFloat);
         Console.WriteLine("double:		{0}
decimal:	{1}",
             firstDouble/secondDouble, firstDecimal/secondDecimal);
         Console.WriteLine(
           "
Remainder(modulus) from integer division:	{0}",
             firstInt%secondInt);

     }
 }

           
          


Uninitialized Values

   

/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/

// with compile error
public class UninitializedValues
 {
    static void Main( )
    {
       int myInt;
       System.Console.WriteLine
       ("Uninitialized, myInt: {0}",myInt);
       myInt = 5;
       System.Console.WriteLine("Assigned, myInt: {0}", myInt);
    }
 }

           
          


Demonstrate the use of readonly variables


   

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

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

//
// ReadOnly.cs -- demonstrate the use of readonly variables
//
//                Compile this program with the following command line
//                    C:>csc ReadOnly.cs
//
namespace nsReadOnly
{
    using System;
    
    public class ReadOnly
    {
        static double DegreeFactor = 1;
        static double MilFactor = 0.05625;
        static public void Main ()
        {
            double degrees = 42;
            
            // 1 degree = 17.77778 mils
            double mils = degrees * 17.77778;
            
            // 1 degree = 0.017453 radians
            double radians = degrees * 0.017453;
            clsArea InDegrees = new clsArea (DegreeFactor);
            InDegrees.Angle = degrees;
            InDegrees.Radius = 50;
            Console.WriteLine ("Area of circle is {0,0:F1}", InDegrees.Area);

            // Radians are the default, so you can use the parameterless 
            // constructor
            clsArea InRadians = new clsArea ();
            InRadians.Angle = radians;
            InRadians.Radius = 50;
            Console.WriteLine ("Area of circle is {0,0:F1}", InRadians.Area);

            clsArea InMils = new clsArea (MilFactor);
            InMils.Angle = mils;
            InMils.Radius = 50;
            Console.WriteLine ("Area of circle is {0,0:F1}", InMils.Area);
        }
    }
    class clsArea
    {
        public clsArea ()
        {
        }
        public clsArea (double factor)
        {
            m_Factor = factor / 57.29578;
        }
        private const double pi = 3.14159;
        private const double radian = 57.29578;
        private readonly double m_Factor = 1;
        public double Angle
        {
            get {return (m_Angle);}
            set {m_Angle = value;}
        }
        public double Radius
        {
            get {return (m_Radius);}
            set {m_Radius = value;}
        }
        private double m_Angle;
        private double m_Radius;
        public double Area
        {
            get
            {
               return (m_Radius * m_Radius * pi * m_Angle * m_Factor /  (2 * pi));
            }
        }
    }
}