A Finalizer in C# (also known as C# destructor) gives you the opportunity to perform cleanup before the class the processed by the Garbage Collector.
So what is C# Destructor?
In C#, destructors are typically used to destroy the instances or objects of a class after they are no longer needed. They are called for classes and cannot be defined in structures. They are invoked directly and cannot be called. The Garbage Collector keeps the track of the objects that are not in any use and are ready for the finalization. It invokes the Finalize () method and set the memory free to be used by other objects of a class. You cannot predict when the Garbage Collector runs by the .NET as it is based on the best optimization algorithms.
The most important part is that the destructor cannot have parameters and it cannot be used with any access modifiers in a program. The Finalize () can be invoked as an independent call in a program. Constructors and Destructors have the same name as of class name but in Destructor, a tilde sign “~” is used before its name.
You can easily take an example of borrowing a box for your temporary use and when your work is completed, you can send it back to the owner so that the other person can use it. Program can have one finalization queue, which needs to be handled carefully when you are making a multi-threaded application in .NET framework. This can affect the performance of an application as it takes longer time to run than Garbage Collector does.
In C#, you can use also use the Dispose() method at the end of the execution of a program to release the unoccupied spaces in the memory.
There can only be a one destructor in a class, which cannot be inherited or overloaded in to any child class. It does not have parameters or modifiers in it i.e. it has no return type and the name is exactly like the class name.
Example of C# Destructor
Here is an example of Destructor in a C# program:
using System;
namespace TestApp.Destructor
{
public class Student
{
// declare variables
public string name;
public int age;
public int gender;
public int department;
// default constructor with no parameters
public Student()
{
}
// parameterized constructor to pass information of a student
public Student(string name, int age, string gender, string department)
{
this.name = name;
this.age = age;
this.gender = gender;
this.department = department;
}
public string getName()
{
return this.name;
}
public int getAge()
{
return this.age;
}
public int getGender()
{
return this.gender;
}
public int getDepartment ()
{
return this.department;
}
//destructor
~Student()
{
Console.WriteLine("Destructor of Student Class has been invoked");
}
}
public class Program
{
// Main method
public static void Main(string[] args)
{
// first object
Student objStudentA = new Student("Harry", 22, "Male", "Computer Science" );
// second object
Student objStudentB = new Student("Diana", 21, "Female", "Business Studies");
// print details of first object of Student Class
Console.WriteLine(objStudentA.getName());
Console.WriteLine(objStudentA.getAge());
Console.WriteLine(objStudentA.getGender());
Console.WriteLine(objStudentA.getDepartment());
// print details of second object of Student Class
Console.WriteLine(objStudentB.getName());
Console.WriteLine(objStudentB.getAge());
Console.WriteLine(objStudentB.getGender());
Console.WriteLine(objStudentB.getDepartment());
}
}
}
Output:
Harry
22
Male
Computer Science
Diana
21
Female
Business Studies
Destructor of Student Class has been invoked
C# Destructor Syntax
Here is a basic syntax of calling destructor in a C# program:
public class ClassName
{
// variables declarations
// Your code …
// methods declarations
// Your code …
// constructor
ClassName ()
{
// Your code
}
// Destructor
~ ClassName ()
{
// Your code
}
}
Use Cases of C# destructors
Destructors are used to perform the last clean-up activity when the garbage collector collects the unused space from the memory in the following cases:
End of Program
The Garbage Collector destroys the class objects and associated memory at the end of execution of a program.
Implicit Call
The Garbage Collector can collect unused memory in the middle of the execution of a program by identifying unused objects.
Explicit Call
The Garbage Collector can be called explicitly by using the ‘GC.Collect’ function so that the unused memory that is hold by the program become free again.
C# Destructors Pros and Cons
Pros
- Destructors have simple syntax and does not require complex programming
- They clean up the code, thus releasing unused space that is occupied by the program and its associated objects automatically
- Destructor can be called in the middle of a program to save the space
- They are also used to release resources like operating system and I/O resources
Cons
- There is only one finalization queue which must be synchronized in multi-threaded application
- Finalize() method takes longer time to release destroyed objects than Garbage Collector
- You cannot predict when the finalize method will be called