15.4 Dispose and Close Methods

Implementing a finalizer gives your type an opportunity to release any external unmanaged resources (such as database connections or file handles) that it may be holding onto. From the perspective of the users of your type, this makes the resource management implicit: they interact with your type, and your type interacts with the resource as needed. However, since GC happens asynchronously and there are no guarantees as to exactly when the finalizers will be called, this level of control may not be sufficient. In these cases, it is good practice to also offer clients explicit resource management controls.

To provide explicit resource management to clients of your type, implement the IDisposable interface. This offers clients a Dispose( ) method they can call to explicitly instruct you to release resources. For types in which Dispose( ) doesn't make sense, provide an explicit Close( ) method that optionally forwards to a private implementation of IDisposable.Dispose. If your type also has a finalizer (via a C# destructor), your Dispose( ) or Close( ) implementation should include a special call to the static SuppressFinalize( ) method on the System.GC type to indicate that the destructor no longer needs to be called. Typically, the real finalizer is written to call the Dispose/Close method, as follows:

using System;
public class Worker : IDisposable {
  int id;
  public Worker(int id) {
    this.id = id;
  // ...
  protected virtual void Dispose(bool disposing) {
    if (disposing) {
      // Not in finalizer, can reference other managed objects here
      Console.WriteLine("Disposing {0}: Releasing managed resources", id);
      // Call Dispose on other managed resources you're holding refs to
    Console.WriteLine("Disposing {0}: Releasing unmanaged resources", id);
    // Release any unmanaged resources you're holding
  public void Dispose( ) {
    GC.SuppressFinalize(this); // Mark this object finalized
  ~Worker( ) {
    Console.WriteLine("Finalizing {0}: Calling Dispose( )", id);
  public static void Main( ) {
    // create a worker and call Dispose when we're done.
    using(Worker w1 = new Worker(1)) {
      // ...
    // create a worker that will get cleaned up when the CLR
    // gets around to it.
    Worker w2 = new Worker(2);

If you run this code, you will see that the finalizer for Worker(1) is never called, since its Dispose( ) method is called once execution leaves the using block, and the Dispose( ) method calls GC.SuppressFinalize( ).

Disposing 1: Releasing managed resources
Disposing 1: Releasing unmanaged resources
Finalizing 2: Calling Dispose( )
Disposing 2: Releasing unmanaged resources

Worker 2 is finalized and disposed when the CLR gets around to it, but it's never given a chance to clean up other objects. The disposable pattern gives you a way to close or dispose of any external objects you might be using, such as an I/O stream, in a safe and consistent manner.

    Part II: Programming with the .NET Framework
    Part IV: API Quick Reference