Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are the differences between constructors and destructors in C#?
A constructor is a special member function that initializes objects when they are created, while a destructor is called when an object goes out of scope or is garbage collected. Understanding the differences between these two fundamental concepts is essential for proper object lifecycle management in C#.
Constructors
A constructor is a special method that is automatically called when an object is created. It has the same name as the class and no return type.
Syntax
class ClassName {
public ClassName() {
// constructor code
}
}
Example
using System;
class Line {
private double length;
public Line() {
Console.WriteLine("Object is being created");
}
public void setLength(double len) {
length = len;
}
public double getLength() {
return length;
}
static void Main(string[] args) {
Line line = new Line();
line.setLength(6.0);
Console.WriteLine("Length of line: {0}", line.getLength());
}
}
The output of the above code is −
Object is being created Length of line: 6
Destructors
A destructor is a special method that is automatically called when an object is destroyed or goes out of scope. It has the same name as the class prefixed with a tilde (~) and cannot have parameters or return values.
Syntax
class ClassName {
~ClassName() {
// destructor code
}
}
Example
using System;
class Line {
private double length;
public Line() {
Console.WriteLine("Object is being created");
}
~Line() {
Console.WriteLine("Object is being deleted");
}
public void setLength(double len) {
length = len;
}
public double getLength() {
return length;
}
static void Main(string[] args) {
Line line = new Line();
line.setLength(6.0);
Console.WriteLine("Length of line: {0}", line.getLength());
}
}
The output of the above code is −
Object is being created Length of line: 6 Object is being deleted
Key Differences
| Constructor | Destructor |
|---|---|
| Called when object is created | Called when object is destroyed or goes out of scope |
| Used to initialize object state | Used to cleanup resources before object destruction |
| Can have parameters | Cannot have parameters |
| Can be overloaded | Cannot be overloaded |
Syntax: ClassName()
|
Syntax: ~ClassName()
|
| Can be called explicitly | Cannot be called explicitly |
Parameterized Constructor Example
using System;
class Rectangle {
private double width, height;
public Rectangle(double w, double h) {
width = w;
height = h;
Console.WriteLine("Rectangle created with width: {0}, height: {1}", width, height);
}
~Rectangle() {
Console.WriteLine("Rectangle with area {0} is being destroyed", width * height);
}
public double getArea() {
return width * height;
}
static void Main(string[] args) {
Rectangle rect = new Rectangle(5.0, 3.0);
Console.WriteLine("Area: {0}", rect.getArea());
}
}
The output of the above code is −
Rectangle created with width: 5, height: 3 Area: 15 Rectangle with area 15 is being destroyed
Conclusion
Constructors initialize objects when created and can accept parameters, while destructors clean up resources when objects are destroyed and cannot have parameters. Constructors are called explicitly during object creation, whereas destructors are called automatically by the garbage collector when objects go out of scope.
