Destructors (C# Programming Guide)
Destructors are used to destruct instances of classes.
Remarks
Destructors cannot be defined in structs. They are only used with classes.
A class can only have one destructor.
Destructors cannot be inherited or overloaded.
Destructors cannot be called. They are invoked automatically.
A destructor does not take modifiers or have parameters.
For example, the following is a declaration of a destructor for the class Car
:
class Car
{
~ Car() // destructor
{
// cleanup statements...
}
}
The destructor implicitly calls Finalize on the object's base class. Therefore, the preceding destructor code is implicitly translated to:
protected override void Finalize()
{
try
{
// cleanup statements...
}
finally
{
base.Finalize();
}
}
This means the Finalize
method is called recursively for all of the instances in the inheritance chain, from the most-derived to the least-derived.
Note
Empty destructors should not be used. When a class contains a destructor, an entry is created in the Finalize
queue. When the destructor is called, the garbage collector is invoked to process the queue. If the destructor is empty, this simply results in a needless loss of performance.
The programmer has no control over when the destructor is called because this is determined by the garbage collector. The garbage collector checks for objects that are no longer being used by the application. If it considers an object eligible for destruction, it calls the destructor (if any) and reclaims the memory used to store the object. Destructors are also called when the program exits.
It is possible to force garbage collection by calling Collect, but in most cases, this should be avoided because it may result in performance issues. For more information, see Forcing a Garbage Collection.
Using Destructors to Release Resources
In general, C# does not require as much memory management as is needed when developing with a language that does not target a runtime with garbage collection. This is because the .NET Framework garbage collector implicitly manages the allocation and release of memory for your objects. However, when your application encapsulates unmanaged resources such as windows, files, and network connections, you should use destructors to free those resources. When the object is eligible for destruction, the garbage collector runs the object's Finalize
method.
Explicit Release of Resources
If your application is using an expensive external resource, it is also recommended that you provide a way to explicitly release the resource before the garbage collector frees the object. You do this by implementing a Dispose
method from the IDisposable interface that performs the necessary cleanup for the object. This can considerably improve the performance of the application. Even with this explicit control over resources, the destructor becomes a safeguard to clean up resources if the call to the Dispose
method failed.
For more details on cleaning up resources, see the following topics:
Example
The following example creates three classes that make a chain of inheritance. The class First
is the base class, Second
is derived from First
, and Third
is derived from Second
. All three have destructors. In Main()
, an instance of the most-derived class is created. When the program runs, notice that the destructors for the three classes are called automatically, and in order, from the most-derived to the least-derived.
class First
{
~First()
{
System.Console.WriteLine("First's destructor is called");
}
}
class Second: First
{
~Second()
{
System.Console.WriteLine("Second's destructor is called");
}
}
class Third: Second
{
~Third()
{
System.Console.WriteLine("Third's destructor is called");
}
}
class TestDestructors
{
static void Main()
{
Third t = new Third();
}
}
Output
Third's destructor is called
Second's destructor is called
First's destructor is called
C# Language Specification
For more information, see the following sections in the C# Language Specification:
1.6.6.6 Destructors
10.2.7.4 Member names reserved for destructors
10.12 Destructors (Classes)
11.3.9 Destructors (Structs)