Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Declaration 
3.
Working of a C# destructor
4.
Characteristics of C# destructor
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Destructors in C#

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

C# is an object-oriented programming language that allows the programmer to create different classes, which are blueprints for creating objects. Constructors are used when initializing or creating an instance of the class, whereas destructors are used to destroy the instance of a class. A destructor in the C# programming language is defined as a method inside the class which is used to destroy the instances of the class when they are no longer required or when the scope of the object ends. Unlike constructors, destructors are unique to their class, that is, a class that can have multiple constructors but only one destructor.

Recommended Topic, Palindrome in C#, singleton design pattern in c#, and Ienumerable vs Iqueryable.

Declaration 

The name of the destructor is always prefixed with a tilde symbol (~). Like a constructor, the name of the destructor is the same as the name of the class. A destructor cannot be overloaded, nor does it accept any parameters.

Syntax

class student
{
// member and member functions
// destructor
~student()
    {
        // body of destructor
    }
}
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Working of a C# destructor

Example

using System; 

namespace destructor {
  
  class student {
    
    // constructor
    public student() {
      Console.WriteLine("Constructor of student class is called.");
    }
    
    // destructor
    ~student() {
      Console.WriteLine("Destructor of student class is called.");
    }

    public static void Main(string [] args) {

      //create an object of student
      student std = new student();
    }
  } 
}


Output

Constructor of student class is called.
Destructor of student class is called.


Explanation 

In the above code, we have first created a class named student. Then we have created a constructor ( student() ) and a destructor ( ~student() ) inside the student class. The constructor is called when an object of the student class is created. Once the scope of the object ends and is no longer needed, the destructor is called implicitly.

Example

using System;  

namespace example {
  
  class student {
    
    // constructor
    public student() {
      Console.WriteLine("Constructor is called.");
    }
    
    string name;
    int rollNo;
    
    void getName() {
      Console.WriteLine("Name: " + name);
    }
    
    void getRollNo() {
      Console.WriteLine("Roll No : " + rollNo);
    }

    // destructor
    ~student() {
      Console.WriteLine("Destructor is called.");
    }

    public static void Main(string [] args) {

      // creates object of Person
      student std = new student();

      std.name = "Captain America";
      std.rollNo=12;
      
      std.getName();
      std.getRollNo();
    }
  } 
}


Output

Constructor is called.
Name: Captain America
Roll No : 12
Destructor is called.


Explanation

In the above code, the class named student consist of a constructor student(), a getName method to get the name of the student, a getRollNo method to get the roll no of the student, and a destructor ~student() to destroy the object when the instance is out of scope. When an object of the class student is created, the constructor is called and “Constructor is called” is printed, when the getName method is called, “Name: Captain America” is printed, and similarly when getRollNo method is called,”Roll No : 12” is printed. Finally, just before the program exits, the destructor is called as the object is no longer required, and finally, the statement, “Destructor is called,” is printed.

Characteristics of C# destructor

Some of the important characteristics/features of a C# destructors are:

  • A class can have only one destructor.
  • The name of the destructor always begins with the tilde symbol (~).
  • The name of the destructor should be the same as the name of the class.
  • A destructor cannot have access modifiers and cannot accept any parameters.
  • A destructor cannot have a return type.
  • Destructors cannot be overloaded
  • A destructor is called implicitly by the garbage collector of the .NET Framework.
  • Destructors can only be used with classes, they cannot be defined in structures.
  • Internally, destructors call the Finalize method on the base class of object.

 

Know What is Object in OOPs here in detail.

FAQs

  1. Can we pass arguments to a destructor?
    In the C# programming language, a destructor cannot accept any parameters. Hence we cannot pass arguments to a destructor.
     
  2. How is a destructor different from a constructor based on naming convention?
    Both the constructor and the destructor should have the same name as the class. The difference is that the name of the destructor always begins with a tilde symbol (~), whereas there is no such tilde symbol used as a prefix in the constructor’s name.
     
  3. Can we overload destructors in the C# programming language?
    No, we cannot overload destructors in the C# programming language. Each class can only have a single destructor, i.e., a destructor is unique to a class.

Key Takeaways

In this article, we have extensively discussed what destructors are, how we can declare them, and what are the characteristic of a destructor in C# programming language with examples and their implementation in Visual Studio Code.

We hope that this blog has helped you enhance your knowledge regarding destructors in the C# programming language and if you would like to learn more, check out our articles on understanding the difference between C# and C++, what is the difference between procedural and object-oriented programming and what are abstract classes and methods in Java.

Do upvote our blog to help other ninjas grow. Happy Coding!”

Live masterclass