See Also: IDisposable
public interface IDisposable
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 IDisposable.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 version-breaking change to add the IDisposable interface to an existing class, because it changes the semantics of the class.
C++ programmers should read Destructors and Finalizers in Visual C++. In the .NET Framework version, the C++ compiler provides support for implementing deterministic disposal of resources and does not allow direct implementation of the IDisposable.Dispose method.
IDisposable and the inheritance hierarchy
A base class with subclasses that should be disposable must implement IDisposable as follows:
It should provide one public non-virtual IDisposable.Dispose method and a protected virtual Dispose(Boolean disposing) method.
The IDisposable.Dispose method must call Dispose(true) and should suppress finalization for performance.
The base type should not include any finalizers.
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).
Calling the IDisposable interface
When calling a class that implements the IDisposable interface, use the try/finally pattern to make sure that unmanaged resources are disposed of even if an exception interrupts your application.
Note that you can use the using statement (Using in Visual Basic) instead of the try/finally pattern. For more information, see the Using Statement (Visual Basic) documentation or the using Statement (C# Reference) documentation.
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 22.214.171.124, 126.96.36.199
The members of System.IDisposable are listed below.