BlackWaspTM
C# Programming
.NET 1.1+

C# Constructor Overloading (2)

The seventh article in the C# Object-Oriented Programming tutorial extends the previous description of method overloading with constructor overloading. Using this technique, multiple constructors can be created to choose from during object instantiation.

Using Overloaded Constructors

Overloaded constructors can be used to instantiate objects in exactly the same manner as for classes with a single constructor. During compilation of the code, the compiler compares the signature used for the new object to those available in the class. If there is a perfect match, the corresponding constructor is used. Where there is no signature with the correct parameters, the compiler will look for a constructor that can be used with implicit casting. If no such constructor exists, a compiler error occurs.

We can now test the example code by modifying the program's main method as follows:

static void Main(string[] args)
{
    Rectangle rect = new Rectangle(4, 6);
    Console.WriteLine("Height: {0}", rect.Height);
    Console.WriteLine("Width: {0}", rect.Width);

    Rectangle square = new Rectangle(5);
    Console.WriteLine("Height: {0}", square.Height);
    Console.WriteLine("Width: {0}", square.Width);
}

/* OUTPUT

Rectangle Constructor Called
Height: 4
Width: 6
Square Constructor Called
Height: 5
Width: 5

*/

Constructor Interaction

Constructors can be very complex, performing many initialisation and validation functions for new objects. This can easily lead to large constructors with functionality that is repeated in each overloaded version. This, in turn, can lead to maintenance problems with the code when changes to construction logic are required. This can be minimised by having the constructor call methods within the class to perform common tasks. Another option is to allow code reuse by having the constructors call each other during object instantiation.

Constructor Calling Syntax

To create a constructor that calls an existing constructor, a special syntax is used. The constructor is declared as usual and then a colon character (:) is appended. After the colon the this keyword and the parameter list of the called constructor is provided. Each parameter specified in the call must match one of those in the new constructor or be a literal value.

public Constructor(parameters-1) : this(parameters-2)
{
}

When the new constructor is utilised, the constructor indicated in the this command is executed first, then the code within the new constructor's code block is run. It is possible that the code block is empty where only a transformation of signature is required. In this case, only the original constructor is executed with the specified parameters.

This reuse of code can be demonstrated by modifying the constructor that creates a square Rectangle object as follows:

public Rectangle(int size) : this(size, size)
{
    Console.WriteLine("Square Constructor Called");
}

Running the program now shows the order of execution of constructors. The results are as follows:

Rectangle Constructor Called
Height: 4
Width: 6
Rectangle Constructor Called
Square Constructor Called
Height: 5
Width: 5
30 October 2007