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.
Example program to declare a destructor for a class

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.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Followers

Total Pageviews

Powered by Blogger.

- Copyright © 2013 DevStudent - Metrominimalist - Powered by Blogger - Designed by Johanes Djogan -