Export (0) Print
Expand All
3 out of 4 rated this helpful - Rate this topic

Destructors

Destructors are used to destruct instances of classes. A destructor is declared using the following form:

[attributes] ~ identifier( ) { destructor-body }

where:

attributes (Optional)
Additional declarative information. For more information on attributes and attribute classes, see C# Attributes.
identifier
The identifier is the same as the class name.
destructor-body
The block that contains the statements that destruct the instance of the class.

Remarks

Destructors cannot be used with 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 MyClass:

~ MyClass() 
{
   // Cleanup statements.
}

The destructor implicitly calls the Object.Finalize method 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 that the Finalize method is called recursively for all of the instances in the inheritance chain, from the most derived to the least derived.

The programmer has no control on 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. It considers these objects eligible for destruction and reclaims their memory. Destructors are also called when the program exits.

It is possible to force garbage collection by calling the GC.Collect method, but in most cases, this should be avoided because it may result in performance issues. For more information, see Forcing a Garbage Collection.

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.

// Destructors1.cs
using System;

class First
{
   ~First()
   {
      Console.WriteLine("First's destructor is called");
   }
}

class Second: First
{
   ~Second()
   {
      Console.WriteLine("Second's destructor is called");
   }
}

class Third: Second
{
   ~Third()
   {
      Console.WriteLine("Third's destructor is called");
   }
}

public class MainClass 
{
   public static void Main() 
   {
      Third myObject = new Third();
   }
}

Output

Third's destructor is called
Second's destructor is called
First's destructor is called

Using Destructors to Release Resources

In general, you should not be as concerned about memory management as you would with C++. 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:

See Also

Programming Essentials for Garbage Collection | 8.13 The using statement | Constructors and Destructors

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.