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?
- class Member
- {
- public Member()
- {
- Console.WriteLine("Default Constructor was called.");
- }
-
- public Member(string name)
- {
- Console.WriteLine("Parameterized Constructor was called.");
- }
-
- ~Member()
- {
- Console.WriteLine("Destructor was called.");
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Member member1 = new Member();
-
- Member member2 = new Member("Rahul");
-
- Console.ReadKey();
- }
- }
Default Constructor was called.
Parameterized Constructor 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?
- class Member
- {
- public Member()
- {
- Console.WriteLine("Default Constructor was called.");
- }
-
- public Member(string name)
- {
- Console.WriteLine("Parameterized Constructor was called. => string = " + name);
- }
-
- public Member(int point) : this("Chirag")
- {
- Console.WriteLine("Parameterized Constructor was called. => int = " + point);
- }
-
- public Member(string name, int point) : this()
- {
- Console.WriteLine("Parameterized Constructor was called. => string, int = " + name + " and " + point);
- }
-
- ~Member()
- {
- Console.WriteLine("Destructor was called.");
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Member member1 = new Member();
- Console.WriteLine("------------------------------");
-
- Member member2 = new Member("Rahul");
- Console.WriteLine("------------------------------");
-
- Member member3 = new Member(200);
- Console.WriteLine("------------------------------");
-
- Member member4 = new Member("Sachin", 500);
- Console.WriteLine("------------------------------");
-
- Console.ReadKey();
- }
- }
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
------------------------------
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#.