Constructors vs. Destructors - C#

Introduction

 
In this article, you will learn about the difference between Constructors and Destructors in C#. 
 
Below is an overview of this article:
  • Background
  • Difference between Constructors and Destructors in C#
  • Practical Scenarios
  • Summary 

Background

 
Here is a list of the key differences between Constructors and Destructors in C#. This article will be useful to both beginners and professional C# developers.
 
 

Difference between Constructors and Destructors in C#

 
Category Constructor Destructor
What is the use of Constructors & Destructors? Constructors are used when instantiating a class. Destructors are used to clean up resources used by the object.
What are Constructors & Destructors? The constructor is a special method of the class that is called when a class is instantiated. Destructor is opposite of constructor. It is a special method of the class that is invoked when a class object goes out of scope.
Name of Constructor & Destructor?
Constructor name is same as class name.
~<ClassName> 
Destructor name is same as class name with starting ~.
~<ClassName>
Access Modifiers?
Constructor can have access modifiers.
By default it is public.
Destructor can’t have access modifiers. 
Parameterized Constructor and Destructor?
Constructor can have parameter(s). Destructor can’t have parameter(s).
Types of Constructor and Destructor?
Constructors can be divided into 5 types:
Default Constructor (Parameter Less Constructor), Parameterized Constructor, Copy Constructor, Static Constructor, Private Constructor
 Destructor can’t have different types.
Number of Constructors and Destructors?
Constructors can be multiple in a class.
Destructors can’t be multiple in a class.
A Destructor is unique to its class.
Constructors and Destructors defined in Structures?  Constructors can be defined in Structures.
Destructors can’t be defined in Structures.
When are Constructors and Destructors invoked?
Constructor gets automatically invoked whenever an instance of the class is created.
Destructors are invoked automatically, and cannot be invoked explicitly.
Execution of the destructor for the instance may occur at any time after the instance becomes eligible for destruction.
 

Practical Scenarios

 
Example 1
 
How to create a Constructor and Destructor? 
  1. class Member  
  2. {  
  3.     public Member()  
  4.     {  
  5.         Console.WriteLine("Default Constructor was called.");  
  6.     }  
  7.   
  8.     public Member(string name)  
  9.     {  
  10.         Console.WriteLine("Parameterized Constructor was called.");  
  11.     }  
  12.   
  13.     ~Member()  
  14.     {  
  15.         Console.WriteLine("Destructor was called.");  
  16.     }  
  17. }   
  1. class Program    
  2. {    
  3.     static void Main(string[] args)    
  4.     {    
  5.         Member member1 = new Member();    
  6.                 
  7.         Member member2 = new Member("Rahul");    
  8.     
  9.         Console.ReadKey();    
  10.     }    
  11. }    
Output
 
Default Constructor was called.
Parameterized Constructor was called.
Destructor was called.
Destructor was called.
 
Note
Here, to easily understand the calling sequence I have used Console.WriteLine in Constructors and Destructor. But in real life, these types of Console.WriteLine are not being used in Constructors and Destructor.  
 
Example 2
 
How to perform Constructor Chaining?
  1. class Member  
  2. {  
  3.     public Member()  
  4.     {  
  5.         Console.WriteLine("Default Constructor was called.");  
  6.     }  
  7.   
  8.     public Member(string name)  
  9.     {  
  10.         Console.WriteLine("Parameterized Constructor was called. => string = " + name);  
  11.     }  
  12.   
  13.     public Member(int point) : this("Chirag")  
  14.     {  
  15.         Console.WriteLine("Parameterized Constructor was called. => int = " + point);  
  16.     }  
  17.   
  18.     public Member(string name, int point) : this()  
  19.     {  
  20.         Console.WriteLine("Parameterized Constructor was called. => string, int = " + name + " and " + point);  
  21.     }  
  22.   
  23.     ~Member()  
  24.     {  
  25.         Console.WriteLine("Destructor was called.");  
  26.     }  
  27. }  
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         Member member1 = new Member();  
  6.         Console.WriteLine("------------------------------");  
  7.               
  8.         Member member2 = new Member("Rahul");  
  9.         Console.WriteLine("------------------------------");   
  10.               
  11.         Member member3 = new Member(200);  
  12.         Console.WriteLine("------------------------------");  
  13.   
  14.         Member member4 = new Member("Sachin", 500);  
  15.         Console.WriteLine("------------------------------");  
  16.   
  17.         Console.ReadKey();  
  18.     }  
  19. }  
Output
 
Default Constructor was called.
------------------------------
Parameterized Constructor was called. => string = Arvind
------------------------------
Parameterized Constructor was called. => string = Chirag
Parameterized Constructor was called. => int = 200
------------------------------
Default Constructor was called.
Parameterized Constructor was called. => string, int = Sachin and 500
------------------------------
Destructor was called.
Destructor was called.
Destructor was called.
Destructor was called.
 
Note
Here, member3 and member4 are calling multiple Constructors due to Constructor Chaining.
 

Summary

 
Now, I believe you know the important key difference between Constructors and Destructors in C#.


Similar Articles