Destructors in C#

Destructors in C# are methods inside the class used to destroy instances of that class when they are no longer needed. The Destructor is called implicitly by the .NET Framework’s Garbage collector and therefore programmer has no control as when to invoke the destructor. An instance variable or an object is eligible for destruction when it is no longer reachable.

Important Points:

  • A Destructor is unique to its class i.e. there cannot be more than one destructor in a class.
  • A Destructor has no return type and has exactly the same name as the class name (Including the same case).
  • It is distinguished apart from a constructor because of the Tilde symbol (~) prefixed to its name.
  • A Destructor does not accept any parameters and modifiers.
  • It cannot be defined in Structures. It is only used with classes.
  • It cannot be overloaded or inherited.
  • It is called when the program exits.
  • Internally, Destructor called the Finalize method on the base class of object.


class Example
    // Rest of the class
    // members and methods.

   // Destructor
        // Your code


Example 1:

// C# Program to illustrate how 
// a destructor works
using System;
namespace GeeksforGeeks {
class Complex {
    // Class members, private 
    // by default
    int real, img;
    // Defining the constructor
    public Complex()
        real = 0;
        img = 0;
    // SetValue method sets 
    // value of real and img
    public void SetValue(int r, int i)
        real = r;
        img = i;
    // DisplayValue displays 
    // values of real and img
    public void DisplayValue()
        Console.WriteLine("Real = " + real);
        Console.WriteLine("Imaginary = " + img);
    // Defining the destructor
    // for class Complex
        Console.WriteLine("Destructor was called");
} // End class Complex
// Driver Class
class Program {
    // Main Method
    static void Main(string[] args)
        // Creating an instance of class 
        // Complex C invokes constructor
        Complex C = new Complex();
        // Calling SetValue method using
        // instance C Setting values of 
        // real to 2 and img to 3
        C.SetValue(2, 3);
        // Displaying values of real
        // and imaginary parts
        // Instance is no longer needed
        // Destructor will be called
    } // End Main
} // End class Program


Real = 2
Imaginary = 3
Destructor was called

Explanation: In the above example, the class consists of a constructor Complex(), a SetValue method to set value of the complex class’ instance, a DisplayValue method to display the value of the instance and a Destructor ~Complex() to destroy the object when the instance is no longer required, it prints the message “Destructor was called”, which depends on the programmer what message he/she wants to display or it can also be left blank.

Example 2:

// C# Program to illustrate how 
// a destructor works
using System;
using System.IO;
namespace GeeksforGeeks {
public class Vect {
    // Class members, private
    // by default
    double i, j, k;
    // Defining the constructor
    public Vect()
        i = 0.0;
        j = 0.0;
        k = 0.0;
        Console.WriteLine("An instance of "+
                      "Vect class created");
    // SetVector method sets 
    // the value of i, j, k
    public void SetVector(double iComponent,
                          double jComponent, 
                          double kComponent)
        i = iComponent;
        j = jComponent;
        k = kComponent;
    // FindMagnitude calculates the 
    // value of the vector's magnitude
    public double FindMagnitude()
        double m = 0.0;
        m = Math.Sqrt(i * i + j * j + k * k);
        return m;
    // Defining the Destructor
    // for class Vect
        Console.WriteLine("The instance of"+
                   " Vect class Destroyed");
} // End class Vect
// Driver Class
class Procedure {
    // Main Method
    static void Main(string[] args)
        // Creates an instance of Vect 
        // class Calls the constructor
        Vect V1 = new Vect();
        // SetVector method is called 
        // with values 2.3, 1.5 and 7.0
        V1.SetVector(2.3, -1.5, 7.0);
        // Prints value of magnitude of 
        // vector calculated by the 
        // FindMagnitude method
        Console.WriteLine("Magnitude of the vector "+
            "2.3i-1.5j+7k is " + V1.FindMagnitude());
        // Instance is destroyed 
        // since no longer needed
    } // End Main
} // End class Procedure


An instance of Vect class created
Magnitude of the vector 2.3i-1.5j+7k is 7.51930847884298
The instance of Vect class Destroyed

Explanation: In this example, we have the class Vect which represents a physical quantity a vector. The class consists of three components i, j and k. The methods of the class include the constructor Vect() that sets all components to zero, a SetVector method to set the value of the instance, a FindMagnitude method to calculate the magnitude of the instance with which it is called and a destructor ~Vect() that will destroy the instance V1 when no longer required.

This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment



load comments

Subscribe to Our Newsletter