C# Destructor

C# Destructors are the methods that are useful to destroy the instances that are no longer needed. Since the .Net framework’s garbage collector will implicitly invoke the destructor whenever needed, the programmer has no control over it.

A C# destructor won’t have any return type and modifiers. It doesn’t accept any parameters, and it invokes when the program exits.

Internally, when we call it, the Finalize method inside the object’s base class will invoke. The syntax of the destructor in C# programming language is

Class Program
{
---
---
 ~Program()
  {
  // code
  }
}

Here, ~Program() is a destructor, which should be the same as the class name preceded by the tilde (~) symbol.

Categories C#