C# Destructor

  • C# destructor can be used to destruct the instance of the class.
  • A class can have many overloaded constructors but can only have one destructor.
  • C# destructor cannot be called, inherited or overloaded but it can be invoked while the instance is being destroyed.
  • After the destructor of a derived class is called to release the resource, the destructor of the base class will be called too.
  • A destructor is defined as a character "~" following with the class name and a pair of brackets "()" without any modifiers and parameters.

For example:

class MyClass
{
    ~MyClass()  // destructor
    {
        // Code here
    }
}

In .Net framework, garbage collector is responsible for the objects allocation and release in memory. If an object is not pointed by any reference variables, it is available to be collected by the garbage collector. Whenever garbage collector finds an object which is not used in the application any more, it will call the destructor of the object before the object is destroyed. Nobody can control when garbage collector starts its work but it definitely works when the program exits. To do some testing, we can call GC.Collect() to force garbage collection.

Example 01-63-01

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;

namespace DestructorTest
{
    public class BaseClass
    {
        public BaseClass()
        {
            Console.WriteLine("BaseClass Constructor");
        }

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

    public class Derived : BaseClass
    {
        public Derived()
        {
            Console.WriteLine("Derived Constructor");
        }

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

    public class TestDestructor
    {
        public static void Main()
        {
            Derived a = new Derived();
            a = null;
            GC.Collect();
            Console.Read();
        }
    }
}

Output

BaseClass Constructor
Derived Constructor
Derived Destructor
BaseClass Destructor

Explanation

  • Line 5-16: Define a public base class BaseClass with a default constructor and a destructor.
  • Line 18-29: Define a public derived class Derived with a default constructor and a destructor.
  • Line 33: Start running Main() in TestDestrutor class.
  • Line 35: Create an object of the derived class Derived. As you can see in the result, the constructor in the base class is called first following the constructor in the derived class.
  • Line 36: Release the object.
  • Line 37: Force garbage collection. The destructor of the unused object is called first then the destructor of its base class.

In summary, destructor is always used to release unmanaged resources like log files, network connections or database connections. For the managed resources, the .Net framework will take care of it.