IDisposable Interface
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Implements: None
Represents a mechanism for releasing unmanaged resources.
Syntax
public interface IDisposable
Methods
Dispose()
void Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
Remarks
The IDisposable
interface is used to define a single method, Dispose
, that can be called to release managed and unmanaged resources. Resources that are not managed by the garbage collector (such as file handles, database connections, or graphics handles) must be explicitly released.
Objects that implement IDisposable
should be disposed of when they are no longer needed to prevent resource leaks. The common way to ensure that Dispose
is called is by using a using
statement (in C#) or a Using...End Using
block (in Visual Basic).
using (StreamReader reader = new StreamReader("file.txt")) {
// Use the reader
Console.WriteLine(reader.ReadLine());
} // reader.Dispose() is automatically called here
Requirements
Platform: Supported in .NET Framework 2.0, .NET Framework 3.5 SP1, .NET Framework 4, .NET Framework 4.5, .NET Framework 4.6, .NET Framework 4.7, .NET Framework 4.8, .NET Core 1.0, .NET Core 1.1, .NET Core 2.0, .NET Core 2.1, .NET Core 2.2, .NET Standard 1.0, .NET Standard 1.1, .NET Standard 1.2, .NET Standard 1.3, .NET Standard 1.4, .NET Standard 1.5, .NET Standard 1.6, .NET Standard 2.0, .NET Standard 2.1, .NET 5, and .NET 6 and later.