Disposable pattern
Sample Code
Sample Code
public class Something : IDisposable {
#region Disposable
private bool _disposed;
void IDisposable.Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
protected void Dispose(bool disposing) {
if (!_disposed) {
if (disposing) {
DisposeManagedResources();
}
DisposeUnmanagedResources();
_disposed = true;
}
}
protected virtual void DisposeManagedResources() { }
protected virtual void DisposeUnmanagedResources() { }
~Something() {
Dispose(false);
}
#endregion
}
Notes
Notes
If you’re creating a class with both managed and unmanaged resources, this is the textbook way to implement the disposable pattern.