
Constructors and
Destructors
Chapter 6
Constructors and destructors perform special
functions as members of classes. Constructors are
used to initialize objects. Conversely, you can use
destructors to delete an object when it is not
required.
This chapter introduces the concept of constructors
and destructors. In addition, the chapter explains
how to implement constructors and destructors. The
chapter also explains how constructors and
destructors help in identifying the life cycle of the
object.
In this chapter, you will learn to:
Implement constructors
Implement destructors
Identify the life cycle of an object
Objectives


¤NIIT Constructors and Destructors 6.3
Aconstructor is a special type of method that is invoked when you create a new instance
of a class. A constructor is used to initialize the members of the class. The name of a
constructor is the same as the name of the class that contains it.
A constructor is complementary to a destructor. It is good programming practice to use
both constructors and destructors.
To initialize member variables, a function can be invoked as soon as an object is created.
The following program is an example of writing an initialization function:
using System;
namespace Calc
{
class Calculator
{
static int number1, number2, total;
public void Intialize()
{
number1 = 10;
number2 = 20;
}
public void AddNumber()
{
total = number1 + number2;
}
public void DisplayNumber()
{
Console.WriteLine ("The Total is :{0}", total);
}
public static void Main(string[] args)
{
Calculator c1 = new Calculator ();
c1.Intialize ();
c1.AddNumber ();
c1.DisplayNumber ();
}
}
}
In the preceding example, the c1 object of the Calculator class has to call the
Initialize () member function to initialize the data members number1 and number2.
However, you need to ensure that the initialize function is invoked.
Implementing Constructors
The Need of Constructors

6.4 Constructors and Destructors ¤NIIT
OOPs offers you a solution to overcome the preceding problem related to ensuring for
invoking the initialize function. While designing a class, you can include a special
member function within the class. This member function is executed whenever an object
of the class is created. Such functions are called constructors.
There are two types of constructors, instance constructors and static constructors.
Instance Constructors
An instance constructor is called whenever an instance of a class is created. These
constructors are used to initialize data members of the class. For automatic initialization,
the Calculator class can be rewritten by using a constructor. For example, consider the
following code:
using System;
namespace Calc
{
class Calculator
{
static int number1, number2, total;
// Constructors have same name as the class.
Calculator()
{
number1 = 10;
number2 = 20;
}
public void AddNumber()
{
total = number1 + number2;
}
public void DisplayNumber()
{
Console.WriteLine ("The Total is :{0}", total);
}
public static void Main(string[] args)
{
Calculator c1 = new Calculator ();
c1.AddNumber ();
c1.DisplayNumber ();
}
}
}
Types of Constructors

¤NIIT Constructors and Destructors 6.5
N
ote
N
ote
In the preceding code notice that there is no return type specified to the
Calculator()constructor. This is because constructors do not return values. The variable
number1 is assigned with value 10 and number2 is assigned with value 20 when the
constructor is invoked by the Common Language Runtime (CLR).
If there are no constructors defined for a class, the compiler creates a default constructor.
In such a case, the instantiation of an object of this class invokes the default constructor.
Static Constructors
Static constructors are used to initialize the static variables of a class. These variables are
created using the static keyword and they store values that can be shared by all the
instances of a class. These constructors have an implicit private access. The constructors
will be invoked only once during the execution of a program.
The following example illustrates the implementation of the static constructor:
public class Class1
{
static int number1;
int number2;
static Class1()
{
number1 = 10; //OK. Since Number1 is a static variable in
class1
number2 = 3; //Error. Since Number2 is a non-static variable
in class1
}
}
In actual practice, constructors are used to initialize data members and not for any
input or output operations.
We can have more than one constructor in a class. Chapter 7 discusses this topic in
detail.