Create class


   

/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/
 using System;

 public class MyTime
 {
     // private variables
     private int year;
     private int month;
     private int date;
     private int hour;
     private int minute;
     private int second;

     // public methods
     public void DisplayCurrentMyTime()
     {
         Console.WriteLine(
             "stub for DisplayCurrentMyTime");
     }
 }

 public class Tester
 {
     static void Main()
     {
         MyTime timeObject = new MyTime();
         timeObject.DisplayCurrentMyTime();
     }

 }

           
          


Variable in and out a class


   

/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/
 using System;

 namespace heap
 {
     public class Dog
     {
         public int weight;
     }

    public class TesterClass
    {
       public void Run()
       {
           // create an integer
           int firstInt = 5;

           // create a second integer
           int secondInt = firstInt;

           // display the two integers
           Console.WriteLine("firstInt: {0} secondInt: {1}",
               firstInt, secondInt);

           // modify the second integer
           secondInt = 7;

           // display the two integers
           Console.WriteLine("firstInt: {0} secondInt: {1}",
               firstInt, secondInt);

           // create a dog
           Dog milo = new Dog();

           // assign a value to weight
           milo.weight = 5;

           // create a second reference to the dog
           Dog fido = milo;

           // display their values
           Console.WriteLine("Milo: {0}, fido: {1}",
               milo.weight, fido.weight);

           // assign a new weight to the second reference
           fido.weight = 7;

           // display the two values
           Console.WriteLine("Milo: {0}, fido: {1}",
               milo.weight, fido.weight);
       }

       static void Main()
       {
          TesterClass t = new TesterClass();
          t.Run();
       }
    }
 }

           
          


Illustrates hiding


   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example5_4.cs illustrates hiding
*/


// declare the Car class
class Car
{

  public int yearBuilt;
  public double maximumSpeed;

  public int Age(int currentYear)
  {
    int maximumSpeed = 100;  // hides the field
    System.Console.WriteLine("In Age(): maximumSpeed = " +
      maximumSpeed);
    int age = currentYear - yearBuilt;
    return age;
  }

  public double Distance(double initialSpeed, double time)
  {
    System.Console.WriteLine("In Distance(): maximumSpeed = " +
      maximumSpeed);
    return (initialSpeed + maximumSpeed) / 2 * time;
  }

}


public class Example5_4
{

  public static void Main()
  {

    // create a Car object
    Car redPorsche = new Car();
    redPorsche.yearBuilt = 2000;
    redPorsche.maximumSpeed = 150;

    int age = redPorsche.Age(2001);
    System.Console.WriteLine("redPorsche is " + age + " year old.");
    System.Console.WriteLine("redPorsche travels " +
      redPorsche.Distance(31, .25) + " miles.");

  }

}


           
          


Show name hiding in a derived class


   

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

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

//
// Hide.cs -- Show name hiding in a derived class
//
//            Compile this program with the following command line:
//                C:>csc Hide.cs
//
namespace nsHide
{
    using System;
    using System.Reflection;
    
    public class Hide
    {
        static public void Main ()
        {
            clsBase Base = new clsBase();
            clsDerived Derived = new clsDerived ();
            Base.x = 42;
            Derived.x = 42;
            Console.WriteLine ("For the base class:");
            Console.WriteLine ("	The type stored in clsBase is " + Base.TypeOf());
            Console.WriteLine ("	MathOp () returns {0,0:F3} for {1}", Base.MathOp(42), 42);
            Console.WriteLine ("
For the derived class:");
            Console.WriteLine ("	The type stored in clsDerived is " + Derived.TypeOf());
            Console.WriteLine ("	MathOp () returns {0,0:F3} for {1}", Derived.MathOp(42), 42);
        }
    }
    class clsBase
    {
        protected int m_x;
        public int x
        {
           get {return (x);}
           set {m_x = value;}
        }
        public double MathOp (int val)
        {
            return (Math.Sqrt ((double) val));
        }
        public string TypeOf ()
        {
            return ("integer");
        }
    }
    class clsDerived : clsBase
    {
        new protected double m_x;
        new public double x
        {
           get {return (x);}
           set {m_x = value;}
        }
        new public double MathOp (int val)
        {
            return ((double) (val * val));
        }
        new public string TypeOf ()
        {
            return ("long");
        }
    }
}


           
          


Demonstrate the use of a nested class to contain data


   

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

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

//
// Nested.cs -- demonstrate the use of a nested class to contain data
//
//              Compile this program with the following command line
//                  C:>csc Nested.cs
//
namespace nsReadOnly
{
    using System;
    
    public class Nested
    {
        static double DegreeFactor = 1;
        static double MilFactor = 0.05625;
        static double RadianFactor = 57.29578;
        static public void Main ()
        {
            double angle = 90;
            double radius = 50;
            
            // Declare an instance of the nested class
            clsArea.clsData data = new clsArea.clsData (angle, radius,
                                                        DegreeFactor);
            clsArea InDegrees = new clsArea (data);

            // Change the values to mils
            data.Factor = MilFactor;
            data.Angle = angle * 17.77778;
            clsArea InMils = new clsArea (data);

            // Change the values to radians
            data.Angle = angle / 57.29578;
            data.Factor = RadianFactor;
            clsArea InRadians = new clsArea (data);

            Console.WriteLine ("Area of pie of {0,0:F3} degrees is {1,0:F1}",
                               InDegrees.Data.Angle, InDegrees.Area);
            Console.WriteLine ("Area of pie of {0,0:F3} radians is {1,0:F1}",
                               InRadians.Data.Angle, InRadians.Area);
            Console.WriteLine ("Area of pie of {0,0:F3} mils is {1,0:F1}",
                               InMils.Data.Angle, InMils.Area);
        }
    }
    class clsArea
    {
        public class clsData : ICloneable
        {
            public clsData (double angle, double radius, double factor)
            {
                m_Angle = angle;
                m_Radius = radius;
                m_Factor = factor / 57.29578;
            }
            public double Angle
            {
                get {return(m_Angle);}
                set {m_Angle = value;}
            }

            public double Radius
            {
                get {return(m_Radius);}
                set {m_Radius = value;}
            }
            public double Factor
            {
                get {return(m_Factor);}
                set {m_Factor = value / 57.29578;}
            }
            private double m_Angle = 0;
            private double m_Radius = 0;
            private double m_Factor = 1;
            public object Clone ()
            {
                clsData clone = new clsData (m_Angle, m_Radius,
                                             m_Factor * 57.29578);
                return (clone);
            }
        }

        public clsArea (clsData data)
        {
            // Clone the data object to get a copy for ourselves
            m_Data = (clsData) data.Clone();
        }

        public clsData Data
        {
            get {return (m_Data);}
        }

        private clsData m_Data;
        private const double pi = 3.14159;
        private const double radian = 57.29578;

        public double Area
        {
            get
            {
               return (m_Data.Radius * m_Data.Radius * pi
                       * m_Data.Angle * m_Data.Factor /  (2 * pi));
            }
        }
    }
}


           
          


Multiple constructors in a class definition


   

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

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// Constrct.cs - Demonstrates the use of multiple constructors
//               in a class definition.
//
//               Compile this program with the following command line:
//                   C:>csc Constrct.cs
//
namespace nsConstructor
{
    using System;
    struct POINT
    {
        public POINT (int cx, int cy)
        {
            this.cx = cx;
            this.cy = cy;
        }
        public int cx;
        public int cy;
    }
    public class Constrct
    {
        static public void Main ()
        {
            clsRect rc1 = new clsRect();
            clsRect rc2 = new clsRect (10, 12, 84, 96);
            POINT pt1 = new POINT (10, 12);
            POINT pt2 = new POINT (84, 96);
            clsRect rc3 = new clsRect (pt1, pt2);
        }
    }
    class clsRect
    {
// The following constructor replaces the default constructor
        public clsRect ()
        {
            Console.WriteLine ("Default constructor called");
            m_Left = m_Top = m_Right = m_Bottom  = 0;
        }
        public clsRect (int cx1, int cy1, int cx2, int cy2)
        {
            Console.WriteLine ("Constructor 1 called");
            m_Left = cx1;
            m_Top = cy1;
            m_Right = cx2;
            m_Bottom = cy2;
        }
        public clsRect (POINT pt1, POINT pt2)
        {
            Console.WriteLine ("Constructor 2 called");
            m_Left = pt1.cx;
            m_Top = pt1.cy;
            m_Right = pt2.cx;
            m_Bottom = pt2.cy;
        }
        public POINT UpperLeft
        {
            get {return(new POINT(m_Left, m_Top));}
            set {m_Left = value.cx; m_Top = value.cy;}
        }
        public POINT LowerRight
        {
            get {return(new POINT(m_Right, m_Bottom));}
            set {m_Right = value.cx; m_Bottom = value.cy;}
        }
        private int m_Left;
        private int m_Top;
        private int m_Right;
        private int m_Bottom;
    }
}



           
          


Illustrates nested classes


   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example6_6.cs illustrates nested classes
*/


// declare the Car class
class Car
{

  // declare the Engine class
  public class Engine
  {

    // declare the Engine fields
    public int cylinders;
    public int horsepower;

    // define the Engine method
    public void Start()
    {
      System.Console.WriteLine("Engine started");
    }

  }

  // declare the Car fields
  public string make;
  public Engine engine;  // Car has an Engine

  // define the Car method
  public void Start()
  {
    engine.Start();
  }

}


public class Example6_6
{

  public static void Main()
  {

    // declare a Car object reference named myCar
    System.Console.WriteLine("Creating a Car object");
    Car myCar = new Car();
    myCar.make = "Toyota";

    // Car objects have an Engine object
    System.Console.WriteLine("Creating an Engine object");
    myCar.engine = new Car.Engine();
    myCar.engine.cylinders = 4;
    myCar.engine.horsepower = 180;

    // display the values for the Car and Engine object fields
    System.Console.WriteLine("myCar.make = " + myCar.make);
    System.Console.WriteLine("myCar.engine.cylinders = " +
      myCar.engine.cylinders);
    System.Console.WriteLine("myCar.engine.horsepower = " +
      myCar.engine.horsepower);

    // call the Car object's Start() method
    myCar.Start();

  }

}