A destructor is a special method called automatically during the destruction of an object. Actions executed in the destructor include the following:
Destructors are called explicitly in C++. However, in C# and Java this is not the case, as the allocation and release of memory allocated to objects are implicitly handled by the garbage collector. While destructors in C# and Java (called finalizers) are nondeterministic, C# destructors are guaranteed to be called by the .NET run time. However, Java finalizers have to be explicitly invoked since their invocation is not guaranteed. Key properties of destructors can be summarized as follows:
Get Techopedia delivered to your inbox!