Using
Constructors and Destructors in C# (C Sharp)
Constructors
and destructors are commonly used in most of your code. But there are
certain concepts about them which most of you might not know. This article
will highlight on those concepts and give you an overview about constructors
and destructors using simple examples.
Constructors in C#:
Constructors
are mainly used and triggered while instantiating a class. Here is a simple
example to demonstrate constructors:
class sampleClass {
int member1, member2;
public sampleClass() {
member1= 10;
member2 = 20;
}
public sampleClass(int member1, int member2) {
this.member1 = member1;
this.member2 = member2;
}
public void displayData() {
Console.WriteLine(member1 = {0}, member2 = {1});
}
}
public class testClass {
public static void Main() {
sampleClass obj1 = new sampleClass();
obj1.displayData();
sampleClass obj2 = new sampleClass(100, 200);
obj2.displayData();
}
}
Output of
this code will be:
member1 = 10, member2 = 20
member1 = 100, member2 = 200
In this example,
you have overloaded constructors in sampleClass and triggered constructors
while instantiating the class. This is just a simple example. There are
much more interesting characteristics about constructors. They are mentioned
below:
Even
if no constructors are defined, default constructor will be executed internally
From one constructor, you can call another constructor
Constructors cannot be inherited
From derived class constructor, you can call base class constructor
Constructors can be overloaded as shown in the example above
Constructors of a class can be classified into private constructors,
instance constructors and static constructors. The example shown above
is an example of instance constructor
Constructors should not have a return type since they are already
returning instance of the class
Destructors
in C#:
You already
know that garbage collector does the cleanup for you. But they deal with
only managed resources. If your object is accessing any unmanaged resource,
then you can release then inside the destructor. A destructor can be defined
in a class as shown below:
class sampleClass
{
~sampleClass(){
Console.WriteLine(In Destructor method of sampleClass);
}
}
Certain guidelines
have to be followed when you use destructors in your code:
Unlike
constructors, your class can have only one destructor
You cannot overload destructors
You cannot inherit destructors
You cannot make an explicit call to destructors. Destructors will
be triggered in an automatic way
You cannot associate any access modifiers to destructors
You cannot associate any parameters to the destructors
Constructors
and Destructors Order of Execution during Inheritance:
An interesting
feature about constructors and destructors is their order of execution
when they are involved in inheritance. Consider the following example
which deals with constructors and destructors in multilevel inheritance:
class baseClass
{
public baseClass() {
Console.WriteLine(Executing Constructor of baseClass);
}
~ baseClass() {
Console.WriteLine(Executing Destructor of baseClass);
}
}
class level1:baseClass {
public level1() {
Console.WriteLine(Executing Constructor of level1);
}
~ level1() {
Console.WriteLine(Executing Destructor of level1);
}
}
class level2:level1 {
public level2() {
Console.WriteLine(Executing Constructor of level2);
}
~ level1() {
Console.WriteLine(Executing Destructor of level2);
}
}
class testClass {
public static void Main() {
level2 obj = new level2();
Console.WriteLine(Object Instantiation successful!);
obj = null;
Console.WriteLine(Releasing Object and Forcing Garbage Collection );
GC.Collect();
}
}
Output of
this code will be:
Executing
Constructor of baseClass
Executing Constructor of level1
Executing Constructor of level2
Object Instantiation successful!
Releasing Object and Forcing Garbage Collection
Executing Destructor of level2
Executing Destructor of level1
Executing Destructor of baseClass
Note that
destructor is executed in the reverse order when compared to the constructor
execution. Even if you release object, it is up to the garbage collector
to decide when to clean it. Only when garbage collector does the cleanup,
the destructors will be executed.
Hence to demonstrate the order of execution, garbage collection is forced
to happen as soon as the object is released. This is done by calling GC.Collect()
method. But you should avoid calling this method in your code as it has
a performance overhead.