C# Destructor
Just opposite to a C# constructor, a destructor in C# destructs the objects of classes. It is invoked automatically and is defined only once in a class. A destructor in C# can’t be public, can’t apply modifiers and can’t have parameters.
Example:
using System; public class Cities { public Cities() { Console.WriteLine("Constructed"); } ~Cities() { Console.WriteLine("Destructed"); } } class Details{ public static void Main(string[] args) { Cities c1 = new Cities(); Cities c2 = new Cities(); } } |
Output:
Explanation:
In the above example, we are creating a C# constructor and a C# destructor and are calling it automatically.