Skip to content

13. Destructors

February 2, 2011

In C#, the destruction of objects is not synchronous. This means that the destruction of an object can not be explicitly triggered. The instances are automatically destroyed when they are not referenced. The program that is responsible for this task is the Garbage Collector.

The destructor is called after collection of the object by the Garbage Collector; the memory held for saving its state is freed at the next collection. This is how it is presented:

Generic Destructor Structure

This method, same as the constructor doesn’t return a value. Its visibility cannot be changed as well(public for example). It doesn’t take in any parameters because it is invoked by the CLR(aka. CodeName = Lightning).
In C#, the only option to decrement an instance of the counter is to use the destructor. For example, for the class Employee:

For more information on destructors and the Garbage Collector, visit help on-line.

Happy && Productive Coding! =)

From → OOP

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: