
.NET 1.1+C# Constructors and Destructors (2)
The fourth article in the C# Object-Oriented Programming tutorial examines constructors and destructors. These special methods allow an object to be initialised on instantiation and to perform final actions before it is removed from memory.
Replacing the Default Constructor
The syntax to add a new constructor to a class is similar to that of adding a method. However, the constructor has the same name as the class and does not include a return type. The declaration for the Triangle's new constructor is therefore simply:
To ensure that all triangles will have a height and base-length within the valid range, we will make the class' constructor set both of these properties to one unit for all new Triangle objects. This is achieved by simply setting the underlying private variables in the constructor's code block. Add the following code within the class' code block to add the constructor.
public Triangle()
{
Console.WriteLine("Triangle constructor executed");
_height = _baseLength = 1;
}
NB: The Console.WriteLine command is added to show that the constructor has been executed in the examples. This would generally not be added to a real class definition.
Executing the Constructor
The newly added constructor replaces the default constructor and is executed automatically when a new Triangle is instantiated. This can be tested by adding some code to the console application's Main method. Add the following code and execute the program to test the results and to see that the height and base-length are set correctly.
static void Main(string[] args)
{
Triangle triangle = new Triangle();
Console.WriteLine("Height:\t{0}", triangle.Height);
Console.WriteLine("Base:\t{0}", triangle.BaseLength);
Console.WriteLine("Area:\t{0}", triangle.Area);
}
/* OUTPUT
Triangle constructor executed
Height: 1
Base: 1
Area: 0.5
*/
Parameterised Constructors
The previous example shows the addition of a new constructor to a class. However, this constructor is very limiting as it includes no parameters to control the initialisation process. To enhance the constructor, we can add parameters in the same way as they would be added to any other method. The following constructor adds two parameters so that the height and base-length of the triangle can be specified during instantiation. It set the properties of the class rather than directly setting the private variables to re-use the validation that they provide. Update the constructor with this version.
public Triangle(int height, int baseLength)
{
Console.WriteLine("Triangle constructor executed");
this.Height = height;
this.BaseLength = baseLength;
}
If you try to build or execute the console application you will now receive a compiler error indicating that no constructor is available that accepts zero arguments. This is because the Main method is still trying to use the default constructor. As this has been replaced, the Main method must be adjusted to use the new, parameterised version as follows:
static void Main(string[] args)
{
Triangle triangle = new Triangle(5,8);
Console.WriteLine("Height:\t{0}", triangle.Height);
Console.WriteLine("Base:\t{0}", triangle.BaseLength);
Console.WriteLine("Area:\t{0}", triangle.Area);
}
/* OUTPUT
Triangle constructor executed
Height: 5
Base: 8
Area: 20
*/
13 October 2007