/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // A parameterized constructor. using System; class MyClass { public int x; public MyClass(int i) { x = i; } } public class ParmConsDemo { public static void Main() { MyClass t1 = new MyClass(10); MyClass t2 = new MyClass(88); Console.WriteLine(t1.x + " " + t2.x); } }
Month: February 2011
A simple constructor
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // A simple constructor. using System; class MyClass { public int x; public MyClass() { x = 10; } } public class ConsDemo1 { public static void Main() { MyClass t1 = new MyClass(); MyClass t2 = new MyClass(); Console.WriteLine(t1.x + " " + t2.x); } }
Illustrates a copy constructor
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example5_13.cs illustrates a copy constructor */ // declare the Car class class Car { // declare the fields private string make; private string model; private string color; private int yearBuilt; // define the copy constructor public Car(Car car) { this.make = car.make; this.model = car.model; this.color = car.color; this.yearBuilt = car.yearBuilt; } public Car(string make, string model, string color, int yearBuilt) { this.make = make; this.model = model; this.color = color; this.yearBuilt = yearBuilt; } // define method to display the fields public void Display() { System.Console.WriteLine("make = " + make); System.Console.WriteLine("model = " + model); System.Console.WriteLine("color = " + color); System.Console.WriteLine("yearBuilt = " + yearBuilt); } } public class Example5_13 { public static void Main() { // create a Car object Car myCar = new Car("Toyota", "MR2", "black", 1995); // create a copy of this Car object Car carCopy = new Car(myCar); // display the values for the Car object's fields System.Console.WriteLine("myCar details:"); myCar.Display(); System.Console.WriteLine("carCopy details:"); carCopy.Display(); } }
Illustrates overloaded constructors
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example5_12.cs illustrates overloaded constructors */ // declare the Car class class Car { // declare the fields private string make; private string model; private string color; private int yearBuilt; // define the overloaded constructors public Car() { this.make = "Ford"; this.model = "Mustang"; this.color = "red"; this.yearBuilt = 1970; } public Car(string make) { this.make = make; this.model = "Corvette"; this.color = "silver"; this.yearBuilt = 1969; } public Car(string make, string model, string color, int yearBuilt) { this.make = make; this.model = model; this.color = color; this.yearBuilt = yearBuilt; } // define method to display the fields public void Display() { System.Console.WriteLine("make = " + make); System.Console.WriteLine("model = " + model); System.Console.WriteLine("color = " + color); System.Console.WriteLine("yearBuilt = " + yearBuilt); } } public class Example5_12 { public static void Main() { // create three Car objects using the constructors // defined in the class Car myCar = new Car("Toyota", "MR2", "black", 1995); Car myCar2 = new Car(); Car myCar3 = new Car("Chevrolet"); // display the values for the Car object's fields System.Console.WriteLine("myCar details:"); myCar.Display(); System.Console.WriteLine("myCar2 details:"); myCar2.Display(); System.Console.WriteLine("myCar3 details:"); myCar3.Display(); } }
Shows the order in which constructors and destructors are called in a C# program
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // Order.cs - shows the order in which constructors and destructors // are called in a C# program. // // Compile this program with the following command line: // C:>csc Order.cs // namespace nsOrder { using System; public class clsMainOrder { static public void Main () { clsLastChild child = new clsLastChild (); Console.WriteLine (); } } // // Declare a base class and have its constructor and destructors // print messages. class clsBase { public clsBase () { Console.WriteLine ("Base class constructor called"); } ~clsBase () { Console.WriteLine ("Base class destructor called"); } } // Derive a class from clsBase. Have the constructor and destructor // print messages. class clsFirstChild : clsBase { public clsFirstChild () { Console.WriteLine ("First Child constructor called"); } ~clsFirstChild () { Console.WriteLine ("First Child destructor called"); } } // Derive a class from clsFirstChile. Have the constructor and destructor // print messages as well. class clsLastChild : clsFirstChild { public clsLastChild () { Console.WriteLine ("Last Child constructor called"); } ~clsLastChild () { Console.WriteLine ("Last Child destructor called"); } } }
Illustrates how to define a constructor
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example5_11.cs illustrates how to define a constructor */ // declare the Car class class Car { // declare the fields private string make; private string model; private string color; private int yearBuilt; // define the constructor public Car(string make, string model, string color, int yearBuilt) { System.Console.WriteLine("In Car() constructor"); this.make = make; this.model = model; this.color = color; this.yearBuilt = yearBuilt; } // define a method to display the fields public void Display() { System.Console.WriteLine("Car details:"); System.Console.WriteLine("make = " + make); System.Console.WriteLine("model = " + model); System.Console.WriteLine("color = " + color); System.Console.WriteLine("yearBuilt = " + yearBuilt); } } public class Example5_11 { public static void Main() { // create a Car object using the constructor // defined in the class Car myCar = new Car("Toyota", "MR2", "black", 1995); // display the values for the Car object fields myCar.Display(); } }
Demonstrate invoking a constructor through this
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Demonstrate invoking a constructor through this. using System; class XYCoord { public int x, y; public XYCoord() : this(0, 0) { Console.WriteLine("Inside XYCoord()"); } public XYCoord(XYCoord obj) : this(obj.x, obj.y) { Console.WriteLine("Inside XYCoord(obj)"); } public XYCoord(int i, int j) { Console.WriteLine("Inside XYCoord(int, int)"); x = i; y = j; } } public class OverloadConsDemo1 { public static void Main() { XYCoord t1 = new XYCoord(); XYCoord t2 = new XYCoord(8, 9); XYCoord t3 = new XYCoord(t2); Console.WriteLine("t1.x, t1.y: " + t1.x + ", " + t1.y); Console.WriteLine("t2.x, t2.y: " + t2.x + ", " + t2.y); Console.WriteLine("t3.x, t3.y: " + t3.x + ", " + t3.y); } }