Destructor in C#

Destructor in C# with Example

In this article, I am going to discuss the Destructor in C# with examples. Please read our previous article before proceeding to this article where we discussed the Constructors in C# with examples. As part of this article, we are going to discuss the following pointers related to Destructor.

  1. What is Destructor in C#
  2. When is the destructor called in c#?
  3. When will be the object of a class gets destroyed in C#?
What is Destructor in C#?

The Destructor is also a special type of method present in a class, just like a constructor, having the same name as the class name but prefix with ~ tilde. The constructor in C# is called when the object of the class is created. On the other hand, the destructor in C# is gets executed when the object of the class is destroyed.

The Constructor and destructor methods will exactly have the same name as the class to which they belong. So to differentiate between these two a tilde (~) operator is used before the destructor method.

For example:

Destructor in C#

Note: the most important point that you need to keep in mind is that a destructor method cannot have any parameters as well as cannot be applied with any modifiers.

When is a destructor method called in c#?

A destructor method gets called when the object of the class is destroyed.

When will be the object of a class gets destroyed in C#?

The object of a class in C# will be destroyed by the garbage collector in any of the following cases

Case1: At the end of a program execution each and every object that is associated with the program will be destroyed by the garbage collector.

Case2: The Implicit calling of the garbage collector occurs sometime in the middle of the program execution provided the memory is full so that the garbage collector will identify unused objects of the program and destroys them.

Case 3: The Explicit calling of the garbage collector can be done in the middle of program execution with the help of “GC.Collect()” statement so that if there are any unused objects associated with the program will be destroyed in the middle of the program execution.

Let us see an example for better understanding of Destructor in C#
namespace DestructorExample
{
    class DestructorDemo
    {
        public DestructorDemo()
        {
            Console.WriteLine("constructor object created");
        }
        ~DestructorDemo()
        {
            Console.WriteLine("object is destroyed");
        }
    }
    class Test
    {
        static void Main(string[] args)
        {
            DestructorDemo obj1 = new DestructorDemo();
            DestructorDemo obj2 = new DestructorDemo();
            obj1 = null;
            obj2 = null;
            GC.Collect();
            Console.ReadKey();
        }
    }
}
OUTPUT:

Destructor in C#

In the next article, I am going to discuss the Access Specifiers in C# with examples. Here, In this article, I try to explain the Destructor in C# with an example. I hope this article will help you with your needs. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *