Constructor Chaining

.Constructor Chaining


Constructor Chaining Program Output

.Constructor Chaining

"A C# program which illustrates constructor chaining. Constructor chaining allows for the specification of another constructor to be called directly after the current constructor. The constructors are chained into a stack structure using the :this() designation."

The program contains a class with four constructors containing from zero to three parameters. The constructors are chained together using the :this() designation so that the last constructor in the chain is the first one executed. Each constructor prints out a line identifying when it is executed. The constructors which have parameters use the parameter(s) to set field values. In the Main method four objects are created, each using a different constructor. The execution order is printed as the constructors are called. After each constructor is called from the Main method, the field values for the object are printed.The following C# features are used in the sample code:

  1. Constructors (C# Programming Guide) - Whenever a class or struct is created, its constructor is called. A class or struct may have multiple constructors that take different arguments.
  2. Using Constructors (C# Programming Guide) - When a class or struct is created, its constructor is called. Constructors have the same name as the class or struct, and they usually initialize the data members of the new object.
  3. Constructor Design - Constructors are special methods used to initialize types and create instances of types.

Constructor Chaining

/* Example of Constructor Chaining */
using System;
namespace Cchaining
{
    public class theClass
    {
        public int _x;
        public int _y;
        public int _z;

        public theClass() : this(0) { Console.WriteLine("No Parm Constructor, (_x = {0})", _x); }

        public theClass(int a) : this(a, 0) {Console.WriteLine("One Parm Constructor (_x = {0})", _x); }

        public theClass(int a, int b) : this(a, b, 0) { _x = a; _y = b; Console.WriteLine("Two Parm Constructor (_x = {0}, _y = {1})", _x, _y); }

        public theClass(int a, int b, int c) { _x = a; _y = b; _z = c; Console.WriteLine("Three Parm Constructor (_x = {0}, _y = {1}, _z = {2})", _x, _y, _z); }
    }

    class Program
    {

        static void Main()
        {
            Console.WriteLine("--- No Parameter Constructor: theClass() ---");
            theClass myClass = new theClass();
            Console.WriteLine("-- From Main: myClass._x = {0}", myClass._x); // Prints: _x = 10
            Console.WriteLine("-- From Main: myClass._y = {0}", myClass._y); // Prints: _y = 20
            Console.WriteLine("-- From Main: myClass._z = {0}", myClass._z); // Prints: _z = 30

            Console.WriteLine("\n--- One Parameter Constructor: theClass(100) ---");
            theClass myClass1 = new theClass(100);
            Console.WriteLine("-- From Main: myClass1._x = {0}", myClass1._x); // Prints: _x = 100
            Console.WriteLine("-- From Main: myClass1._y = {0}", myClass1._y); // Prints: _y = 20
            Console.WriteLine("-- From Main: myClass1._z = {0}", myClass1._z); // Prints: _z = 30

            Console.WriteLine("\n--- Two Parameter Constructor: theClass(1000, 2000) ---");
            theClass myClass2 = new theClass(1000, 2000);
            Console.WriteLine("From Main: myClass2._x = {0}", myClass2._x); // Prints: _x = 1000
            Console.WriteLine("From Main: myClass2._y = {0}", myClass2._y); // Prints: _y = 2000
            Console.WriteLine("From Main: myClass2._z = {0}", myClass2._z); // Prints: _z = 30


            Console.WriteLine("\n--- Three Parameter Constructor: theClass(10000, 20000, 30000) ---");
            theClass myClass3 = new theClass(10000, 20000, 30000);
            Console.WriteLine("From Main: myClass3._x = {0}", myClass3._x); // Prints: _x = 10000
            Console.WriteLine("From Main: myClass3._y = {0}", myClass3._y); // Prints: _y = 20000
            Console.WriteLine("From Main: myClass3._z = {0}", myClass3._z); // Prints: _z = 30000

            Console.WriteLine();
        }
    }
}