- Back to Home »
- C#.NET »
- C#.NET Destructor
Posted by :
Sudhir Chekuri
Tuesday, 5 January 2016
- · Destructor is a special method which has the same name of the class.
- · It is invoked automatically when an object of that class is cleared.
Program
using System;
namespace destructor
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
Console.ReadKey();
}
~Program()
{
Console.WriteLine("destroyed");
Console.ReadKey();
}
}
}
Output
destroyed
In the above example destructor with the same name of the class i.e., Program is created using tilt symbol before the method name as below.
~ Program()
{
}
This destructor is invoked automatically when the memory of that class Program is cleared.
So, the message in destructor method is displayed before the output screen gets closed.