Open In App

Destructors in C#

Last Updated : 15 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

  • 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.

Syntax

class Example
{
// Rest of the class constructor ,
// members and methods.

// Destructor
~Example()
{
// Your code
}
}

A Destructor is distinguished from a constructor by the tilde symbol (~) prefixed to its name. It does not take any parameters and does not return anything, not even void. Like a constructor, a destructor is unique to its class, meaning that a class can have only one destructor.

Example 1:

C#
// How to use Destructor
using System;

class Geeks 
{
    // Constructor
    public Geeks() 
    {
        Console.WriteLine("Object Created.");
    }

    // Destructor
    ~Geeks() 
    {
        Console.WriteLine("Object Destroyed.");
    }

    public void DisplayMessage() 
    {
        Console.WriteLine("Message Printed.");
    }

    public static void Main(string[] args)
    {
        // Create an instance of Geeks
        Geeks g = new Geeks();
      
        // Destructor will be called when
      	// g goes out of scope
        g.DisplayMessage();
    }
}

Output
Object Created.
Message Printed.
Object Destroyed.

Explanation: Constructor is called when the new object is created, after which all the operations are performed. At the end, before Main method is exited all the object elements are destroyed.

Cases to understand before using Destructors

  • 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, the Destructor is called the Finalize method on the base class of the object.

Example 2:

C#
// Demonstration of Destructor
using System;

class Complex
{
    private int real, img;

    // Constructor
    public Complex()
    {
        real = 0;
        img = 0;
    }

    // Method to set values
    public void SetValue(int r, int i)
    {
        real = r;
        img = i;
    }

    // Method to display values
    public void DisplayValue()
    {
        Console.WriteLine($"Real = {real}, Imaginary = {img}");
    }

    // Destructor
    ~Complex()
    {
        Console.WriteLine("Object Destroyed");
    }
}

class Geeks
{
    static void Main(string[] args)
    {  
        // Create instance
        Complex c = new Complex(); 
      
        c.SetValue(2, 3);         
        c.DisplayValue();  
      
        // Destructor will be called automatically
      	// when 'c' goes out of scope.
    }
}

Output
Real = 2, Imaginary = 3
Object Destroyed.

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.

Destructor is mainly used to release resources like memory, file handles, or database connections, ensuring that no resources are left hanging when an object goes out of scope.



Next Article
Article Tags :

Similar Reads

  翻译: