Constructors and Destructors
Contents |
Constructors
When a class is declared as in the following code, we only say that we want to have an instance of the class in our program. We have not yet created the class, only that we have allocated some program memory.
Car FordCapri;
To do this we must instantiate an instance of the class, this is done using the new keyword, as in the following code:
Car FordCapri = new Car();
You can see that after the new keyword we called a method with the same name as the class. This is called its constructor. A constructor is a method that is invoked upon instantiation of a class, known as "instance constructors". An instance constructor is a member that implements the actions required to initialize an instance of a class.
Every constructor has the same defining layout:
[access-modifier] constructor_name (parameters)
{
// constructor body
}
The access-modifier is optional and can be private, public, protected or internal. If no modifier is supplied, then the default is private.
The constructor_name of a constructor must be the same as the name of the class.
A constructor can take zero or more arguments as parameters. A constructor with zero arguments (that is no-arguments) is known as a default constructor. A class can have more than one constructor, by giving each constructor a different set of parameters. This gives the user the ability to initialise the object in more than one way.
The constructor body can be used to initialise the object into a known starting state.
In, general, a constructor’s access modifier is public, this is because the class that is creating an instance of this class, needs to be able to access the constructor of the class.
public Car() { . . . }
Static constructors
A static constructor is used to initialize any static data members. It is called automatically before the first instance is created or any static members are referenced. A static constructor does not take access modifiers or have parameters and therefore cannot be called using the new keyword.
Static constructors have the following properties:
- A static constructor does not take access modifiers or have parameters.
- Accessibility defaults to the same accessibility as the class itself.
- A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
- A static constructor cannot be called directly.
- A static constructor cannot be overloaded.
- A static constructor is not inheritable.
Destructors
Each class also requires a destructor, this is the code that is called to clean up the object when it has been finished with. However, if you do not provide one, then the Garbage Collector will deal with freeing up the memory for you. Any object that you create in the class, should be deleted from the class as a general rule of thumb.
A destructor is defined pretty much in the same way as a constructor, but has a tilde (~) in front of the method name.
public ~Car() { . . . }
Destructors have the following properties:
- Destructors cannot be inherited or overloaded.
- Destructors cannot be called. They are invoked automatically.
- A destructor does not take modifiers or have parameters.
Note
- Empty destructors should not be used. When a class contains a destructor, an entry is created in the Finalize queue. When the destructor is called, the garbage collector is invoked to process the queue. If the destructor is empty, this simply results in a needless loss of performance.
- You should only create a destructor if it is really necessary. Otherwise let the memory management clean up behind you.
MSDN references
|

