---
title: "Destructor in C#"  
description: "A destructor is a function with the same name as the name of a class but starting with the character ~. A constructor gets called at birth whereas a d"  
author: "Amit Singh"  
published: 2010-11-06  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/45/destructor-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Destructor in C#

A destructor is a [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) with the same name [as the name](https://www.mindstick.com/forum/34352/the-term-update-database-is-not-recognized-as-the-name-of-a-cmdlet-function) of a class but [starting](https://www.mindstick.com/blog/12024/things-you-need-to-know-when-starting-your-own-business) with the [character](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-character) ~. A constructor gets called at birth whereas a destructor gets called at death.\

In C# we cannot decide when the destructor gets called. This [decision](https://answers.mindstick.com/qa/116223/apple-s-upgrade-decision-hundreds-of-millions-of-iphones-affected) to call the destructor is made by a program within C# called the [garbage collector](https://www.mindstick.com/forum/158204/what-are-the-benefits-and-drawbacks-of-using-a-garbage-collector-for-memory-management). \
The [concept](https://www.mindstick.com/blog/79/routing-concept-in-dot-net) first gained [currency](https://www.mindstick.com/articles/311850/currency-wars-what-they-are-and-how-they-work) with the advent of Java. In Java and C# we cannot remove our [objects](https://www.mindstick.com/forum/145447/what-are-objects) from memory. Thus it is for the garbage collector to decide when to call the destructor.\

##### For Example:

\

```
public class Sample
{
public static void Main()
{
aa a = new aa();
}
}
public class aa
{
public aa()
{
System.Console.WriteLine("Constructor ");
}
~aa()
{
System.Console.WriteLine("Destructor");
}
}
```

##### Output

Constructor\
Destructor\

---

Original Source: https://www.mindstick.com/blog/45/destructor-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
