IDisposable
Context: IDisposable is an interface that provides a mechanism for releasing unmanaged resources (file handles, database connections, sockets) in a deterministic manner. The .NET GC only cleans up managed memory; unmanaged resources must be explicitly released. IDisposable defines a single method Dispose(). When you implement IDisposable, you should also implement a finalizer (destructor) as a safety net.
Usage Example
Section titled “Usage Example”using System;using System.IO;
public class ResourceHolder : IDisposable{ private FileStream _fileStream; private bool _disposed = false;
public ResourceHolder(string path) { _fileStream = File.OpenRead(path); }
public void Dispose() { Dispose(true); GC.SuppressFinalize(this); }
protected virtual void Dispose(bool disposing) { if (!_disposed) { if (disposing) { _fileStream?.Dispose(); } _disposed = true; } }
~ResourceHolder() { Dispose(false); }}Output console
Section titled “Output console”// No direct output; the Dispose method is called automatically at the end of a using block.Important notes
Section titled “Important notes”- Always call
Dispose()when you are done with anIDisposableobject. - Use the
usingstatement to guarantee disposal even if exceptions occur.
Real‑world usage example
Section titled “Real‑world usage example”File I/O – Every StreamReader, FileStream, or SqlConnection implements IDisposable. Always dispose them to free operating system handles.
See .NET docs on IDisposable.