How many destructors can we have in one class in C#?

In C#, a destructor is a special method used to clean up resources when an object is destroyed. The answer to how many destructors a class can have is simple: only one destructor per class.

Destructors are also called finalizers because they are automatically converted to Finalize() method calls by the compiler.

Syntax

Following is the syntax for declaring a destructor −

class ClassName {
   ~ClassName() {
      // cleanup code
   }
}

Properties of Destructors

  • Destructors do not take any parameters

  • Destructors begin with a tilde symbol (~)

  • Destructors cannot be defined in structs

  • Destructors cannot be called explicitly ? they are invoked automatically by the garbage collector

  • Destructors implicitly call Finalize() on the base class

Example of Single Destructor

using System;

class Demo {
   public Demo() {
      Console.WriteLine("Constructor called");
   }
   
   ~Demo() {
      Console.WriteLine("Destructor called");
   }
}

class Program {
   static void Main() {
      Demo obj = new Demo();
      obj = null;
      GC.Collect();
      GC.WaitForPendingFinalizers();
   }
}

The output of the above code is −

Constructor called
Destructor called

How Destructors Work Internally

The destructor is implicitly translated by the compiler into a Finalize() method. The following destructor −

~Demo() {
   // cleanup statements
}

Is automatically converted to −

protected override void Finalize() {
   try {
      // cleanup statements
   }
   finally {
      base.Finalize();
   }
}

Multiple Destructors Cause Compilation Error

If you attempt to declare more than one destructor, the compiler will throw an error: "'Demo' already defines a member called '~Demo'".

Example

using System;

class Demo {
   ~Demo() {
      Console.WriteLine("First destructor");
   }
   
   // This will cause compilation error
   /*
   ~Demo() {
      Console.WriteLine("Second destructor");
   }
   */
}

class Program {
   static void Main() {
      Demo obj = new Demo();
      Console.WriteLine("Program executed successfully with single destructor");
   }
}

The output of the above code is −

Program executed successfully with single destructor

Why Only One Destructor?

C# allows only one destructor per class because:

  • Destructors cannot be overloaded (they take no parameters)

  • Multiple destructors would create ambiguity about cleanup order

  • The garbage collector needs a single, well-defined cleanup method

Conclusion

A C# class can have exactly one destructor. Attempting to define multiple destructors results in a compilation error. Destructors are automatically called by the garbage collector and are internally converted to Finalize() methods for proper resource cleanup.

Updated on: 2026-03-17T07:04:36+05:30

550 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements