What are destructors in C #

What are destructors in C #

What are destructors in C #

In C # Class is instantiated when Constructors are used but when an Object of a Class is deleted Class Destructors are automatically invoked.

  • There can be only one destructor for a class.
  • A Destructor is not called / cannot be called, it is invoked automatically.
  • A Destructor cannot be modified using modifiers
  • A Destructor cannot have parameters

A Destructor Shares same name with the Class but a “ ~ ”  TILDE is used with it

Let’s see an example:

Classname | ~Classname

Example Program in C#

class Person
{
public Person() {
Console.WriteLine(“This is a Constructor”);
}
~Person() {
Console.WriteLine(“This is a Destructor”);
}
}
static void Main(string[] args) {
Person Rajat = new Person();
}
/*Outputs Will Be
This is a Constructor
This is a Destructor
/*

Social Share

Leave a Reply

Your email address will not be published. Required fields are marked *