What are destructors in C# programs?

A destructor is a special member function of a class that is executed whenever an object of its class goes out of scope or is garbage collected. It performs cleanup operations before the object is destroyed from memory.

A destructor has exactly the same name as the class with a prefixed tilde (~), and it cannot have parameters or return values. It is automatically called by the garbage collector, not directly by the programmer.

Syntax

Following is the syntax for declaring a destructor −

~ClassName() {
    // cleanup code
}

Here is a comparison with a constructor −

public Demo() {  // constructor
    Console.WriteLine("Object is being created");
}

~Demo() {  // destructor
    Console.WriteLine("Object is being deleted");
}

Constructor vs Destructor Constructor ClassName() ? Called when object created ? Initialize resources ? Can have parameters Destructor ~ClassName() ? Called before object destroyed ? Cleanup resources ? No parameters allowed

Key Rules of Destructors

  • A destructor cannot be called directly ? it is invoked automatically by the garbage collector.

  • A destructor cannot have parameters or access modifiers.

  • A class can have only one destructor.

  • Destructors cannot be inherited or overloaded.

  • The timing of destructor execution is not guaranteed ? it depends on garbage collection.

Example

using System;

namespace LineApplication {
    class Line {
        private double length; // Length of a line

        public Line() { // constructor
            Console.WriteLine("Object is being created");
        }

        ~Line() { //destructor
            Console.WriteLine("Object is being deleted");
        }

        public void setLength( double len ) {
            length = len;
        }

        public double getLength() {
            return length;
        }

        static void Main(string[] args) {
            Line line = new Line();
    
            // set line length
            line.setLength(6.0);
            Console.WriteLine("Length of line : {0}", line.getLength());
        }
    }
}

The output of the above code is −

Object is being created
Length of line : 6
Object is being deleted

Using Destructors for Resource Management

Example

using System;

class FileManager {
    private string fileName;

    public FileManager(string name) {
        fileName = name;
        Console.WriteLine("Opening file: " + fileName);
    }

    ~FileManager() {
        Console.WriteLine("Closing file: " + fileName);
        Console.WriteLine("Resources cleaned up");
    }

    public void ProcessFile() {
        Console.WriteLine("Processing file: " + fileName);
    }
}

class Program {
    public static void Main() {
        FileManager fm = new FileManager("data.txt");
        fm.ProcessFile();
        Console.WriteLine("Main method ending");
        GC.Collect(); // Force garbage collection for demo
        GC.WaitForPendingFinalizers();
    }
}

The output of the above code is −

Opening file: data.txt
Processing file: data.txt
Main method ending
Closing file: data.txt
Resources cleaned up

Destructor vs IDisposable Pattern

Destructor IDisposable.Dispose()
Called automatically by garbage collector Called explicitly by programmer using using or Dispose()
Timing is not guaranteed Called immediately when needed
Good for safety net cleanup Preferred for deterministic resource cleanup
Cannot throw exceptions Can throw exceptions

Conclusion

Destructors in C# provide automatic cleanup when objects are garbage collected. While useful as a safety net, the IDisposable pattern is generally preferred for deterministic resource cleanup. Destructors are called automatically by the garbage collector and cannot have parameters or be called directly.

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

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements