IDisposable Interface


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Provides a mechanism for releasing unmanaged resources.

To browse the .NET Framework source code for this type, see the Reference Source.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public interface class IDisposable


Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.


To view the .NET Framework source code for this type, see the Reference Source. You can browse through the source code online, download the reference for offline viewing, and step through the sources (including patches and updates) during debugging; see instructions.

The primary use of this interface is to release unmanaged resources. The garbage collector automatically releases the memory allocated to a managed object when that object is no longer used. However, it is not possible to predict when garbage collection will occur. Furthermore, the garbage collector has no knowledge of unmanaged resources such as window handles, or open files and streams.

Use the Dispose method of this interface to explicitly release unmanaged resources in conjunction with the garbage collector. The consumer of an object can call this method when the object is no longer needed.


It is a breaking change to add the IDisposable interface to an existing class. Because pre-existing consumers of your type cannot call Dispose, you cannot be certain that unmanaged resources held by your type will be released.

Because the IDisposable::Dispose implementation is called by the consumer of a type when the resources owned by an instance are no longer needed, you should either wrap the managed object in a SafeHandle (the recommended alternative), or you should override Object::Finalize to free unmanaged resources in the event that the consumer forgets to call Dispose.


In the .NET Framework, the C++ compiler supports deterministic disposal of resources and does not allow direct implementation of the Dispose method.

For a detailed discussion about how this interface and the Object::Finalize method are used, see the Garbage Collection and Implementing a Dispose Method topics.

Implement IDisposable only if you are using unmanaged resources directly. If your app simply uses an object that implements IDisposable, don't provide an IDisposable implementation. Instead, you should call the object's IDisposable::Dispose implementation when you are finished using it. Depending on your programming language, you can do this in one of two ways:

  • By using a language construct such as the using statement in C# and Visual Basic.

  • By wrapping the call to the IDisposable::Dispose implementation in a try/catch block.


Documentation for types that implement IDisposable note that fact and include a reminder to call its Dispose implementation.

If your language supports a construct such as the using statement in C# and the Using statement in Visual Basic, you can use it instead of explicitly calling IDisposable::Dispose yourself. The following example uses this approach in defining a WordCount class that preserves information about a file and the number of words in it.

No code example is currently available or this language may not be supported.

The using statement is actually a syntactic convenience. At compile time, the language compiler implements the intermediate language (IL) for a try/catch block.

For more information about the using statement, see the Using Statement (Visual Basic) or using Statement (C# Reference) topics.

If your programming language does not support a construct like the using statement in C# or Visual Basic, or if you prefer not to use it, you can call the IDisposable::Dispose implementation from the finally block of a try/catch statement. The following example replaces the using block in the previous example with a try/catch/finally block.

No code example is currently available or this language may not be supported.

For more information about the try/finally pattern, see Try...Catch...Finally Statement (Visual Basic), try-finally (C# Reference), or try-finally Statement (C).

You should implement IDisposable only if your type uses unmanaged resources directly. The consumers of your type can call your IDisposable::Dispose implementation to free resources when the instance is no longer needed. To handle cases in which they fail to call Dispose, you should either use a class derived from SafeHandle to wrap the unmanaged resources, or you should override the Object::Finalize method for a reference type. In either case, you use the Dispose method to perform whatever cleanup is necessary after using the unmanaged resources, such as freeing, releasing, or resetting the unmanaged resources.


If you are defining a base class that uses unmanaged resources and that either has, or is likely to have, subclasses that should be disposed, you should implement the IDisposable::Dispose method and provide a second overload of Dispose, as discussed in the next section.

A base class with subclasses that should be disposable must implement IDisposable as follows. You should use this pattern whenever you implement IDisposable on any type that isn't sealed (NotInheritable in Visual Basic).

  • It should provide one public, non-virtual Dispose() method and a protected virtual Dispose(Boolean disposing) method.

  • The Dispose() method must call Dispose(true) and should suppress finalization for performance.

  • The base type should not include any finalizers.

The following code fragment reflects the dispose pattern for base classes. It assumes that your type does not override the Object::Finalize method.

No code example is currently available or this language may not be supported.

If you do override the Object::Finalize method, your class should implement the following pattern.

No code example is currently available or this language may not be supported.

Subclasses should implement the disposable pattern as follows:

  • They must override Dispose(Boolean) and call the base class Dispose(Boolean) implementation.

  • They can provide a finalizer if needed. The finalizer must call Dispose(false).

Note that derived classes do not themselves implement the IDisposable interface and do not include a parameterless Dispose method. They only override the base class Dispose(Boolean) method.

The following code fragment reflects the dispose pattern for derived classes. It assumes that your type does not override the Object::Finalize method.

No code example is currently available or this language may not be supported.

The following example demonstrates how to create a resource class that implements the IDisposable interface.

#using <System.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;

// The following example demonstrates how to create a class that 
// implements the IDisposable interface and the IDisposable.Dispose
// method with finalization to clean up unmanaged resources. 
public ref class MyResource: public IDisposable

   // Pointer to an external unmanaged resource.
   IntPtr handle;

   // A managed resource this class uses.
   Component^ component;

   // Track whether Dispose has been called.
   bool disposed;

   // The class constructor.
   MyResource( IntPtr handle, Component^ component )
      this->handle = handle;
      this->component = component;
      disposed = false;

   // This method is called if the user explicitly disposes of the
   // object (by calling the Dispose method in other managed languages, 
   // or the destructor in C++). The compiler emits as a call to 
   // GC::SuppressFinalize( this ) for you, so there is no need to 
   // call it here.
      // Dispose of managed resources.

      // Call C++ finalizer to clean up unmanaged resources.

      // Mark the class as disposed. This flag allows you to throw an
      // exception if a disposed object is accessed.
      disposed = true;

   // Use interop to call the method necessary to clean up the 
   // unmanaged resource.
   static Boolean CloseHandle( IntPtr handle );

   // The C++ finalizer destructor ensures that unmanaged resources get
   // released if the user releases the object without explicitly 
   // disposing of it.
      // Call the appropriate methods to clean up unmanaged 
      // resources here. If disposing is false when Dispose(bool,
      // disposing) is called, only the following code is executed.
      CloseHandle( handle );
      handle = IntPtr::Zero;


void main()
   // Insert code here to create and use the MyResource object.
   MyResource^ mr = gcnew MyResource((IntPtr) 42, (Component^) gcnew Button());

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top