In the c#, Destructors are methods inside the class used to destroy instances of that class when they are no longer needed or destructor works opposite to constructor, It destructs the objects of classes.
using System;
public class EmployeeInfo
{ public EmployeeInfo() {
Console.WriteLine("Constructor Example");
} ~EmployeeInfo()
{
Console.WriteLine("Destructor Example");
}
}
public class AnotherClassEmployeeInfo{
public static void Main(string[] args)
{
EmployeeInfo obj = new EmployeeInfo();
EmployeeInfo obj1= new EmployeeInfo();
}
}
Output
Constructor Example
Constructor Example
Destructor Example
Destructor Example