Destructor

Destructors are used to destruct instances of classes. It can only be used with classes and not with structs. A class can only have a single destructor, which neither can be called nor inherited or overloaded. They are invoked automatically as and when determined by the garbage collector. A destructor cannot have access modifiers or parameters. 

The destructor implicitly calls the Object.Finalize method on the object's base class. This means that the Finalize method is called recursively for all of the instances in the inheritance chain, from the most derived to the least derived. 

The destructor is called as and when determined by the garbage collector. The garbage collector checks for objects that are no longer being used by the application. These objects are considered eligible for destruction and the GC reclaims their memory. Destructors are also called when the program exits.

Related Post: Constructors

No comments:

Post a Comment