---
title: "What is destructor in C# and Working?"  
description: "Destructors are used to destruct instances of classes.  In this article I will explain how different C# destructors are when compared to C++ destructo"  
author: "Sushant Mishra"  
published: 2017-09-14  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/12587/what-is-destructor-in-c-sharp-and-working  
category: "c#"  
tags: ["c#", "oops"]  
reading_time: 2 minutes  

---

# What is destructor in C# and Working?

Destructors are used to destruct [instances](https://answers.mindstick.com/qa/92508/how-can-you-hide-the-sql-server-instances) of classes. In this [article](https://yourviews.mindstick.com/view/81489/kashmir-now-after-an-year-of-abrogation-of-article-370) I will explain how different C# destructors are when compared to C++ destructors.

In C# you can never call them, the reason is one cannot destroy an object. It’s the [Garbage collector](https://www.mindstick.com/forum/155585/what-is-garbage-collector) (.Net [Framework](https://yourviews.mindstick.com/view/81101/time-for-improvement-in-healthcare-facility-framework)) who has the control over the destructor (in C#).

**Syntax for destructor**

~classname ()

{

}

~ (we say it tilled)

Now are going know it characteristics one by one

**Characteristics of Destructor**

1. Destructors (~) cannot be defined in Struts.
2. Destructors (~) are only used with classes.
3. Destructor cannot be inherited or overloaded.
4. Destructor does not take modifiers or have parameters.
5. It cannot be called. Destructor are called automatically.
6. An [instance](https://www.mindstick.com/forum/155658/testing-connection-to-cloud-sql-instance-with-a-mysql-client-from-vm-instance) becomes eligible for destruction when it is no longer possible for any code to use the instance.
7. We have no control over when destructor is called because this is determined by Garbage Collector.
8. Destructor is called when program exits.
9. Execution of the destructor for the instance may occur at any time after the

instance becomes eligible for destruction.

Its execution order is [reverse](https://www.mindstick.com/blog/63740/what-are-the-most-effective-and-safest-thanks-to-reverse-erectile-dysfunction) in comparison to constructor,Let us take an example

to see this

```
using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace ConsoleApplication2{   
class Program   
{       
class Destructor_1       
{     ~Destructor_1()      {        System.Console.WriteLine("First's destructor is called");      }       
}       
class Destructor_2 : Destructor_1       
{   ~Destructor_2()     {       System.Console.WriteLine("Second's destructor is called");     }       
}     
class Destructor_3 : Destructor_2       
  {     ~Destructor_3()       {         System.Console.WriteLine("Third's destructor is called");       }       
  }       
class TestDestructors       
{     static void Main()      {       Destructor_3 t = new Destructor_3();      }       
}      
   
}} 
```

**output**

![What is destructor in C# and Working?](https://www.mindstick.com/mindstickarticle/bed651ac-3b49-4c8e-84ef-7be81ec351e8/images/49857f46-a175-4a00-8628-0ca37f5fcc17.png)

**Working of garbage collector**

- Main function of Garbage collector is to checks for objects/unused that are no longer being used by application. If it treated an object eligible for destruction. It calls the destruction and reclaims the memory used to store the object.

- GC keeps tracks of all the objects and ensures that each object gets destroyed once.
- GC ensures that objects, which are being referenced, are not destroyed.
- Garbage collector destroys the objects only when needed.

---

Original Source: https://www.mindstick.com/articles/12587/what-is-destructor-in-c-sharp-and-working

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
