Thursday, August 24, 2017

Destructor

Destructor use for deallocating the dynamically allocate.When a program needs to destroy an object there needs destructor. Destructor has to the same name as the class name.It's automatically called when the object goes out of scope.

Destructor feature :

  • Destructor can't be overloaded or inherited.
  • Destructor can't be used with the class.
  • Destructor doesn't take parameter.
  • Destructor invoked automatically.

Example:


class Z
{
    public Z()
    {
        Console.WriteLine("Constructor");
    }

    ~Z()
    {
        Console.WriteLine("Destructor");
    }
}

class Program
{
    static void Main()
    {
        Z x = new Z();
    }
}

No comments:

Post a Comment