Skip to content

Instantly share code, notes, and snippets.

@thinktainer
Created August 31, 2013 19:22
Show Gist options
  • Save thinktainer/6400082 to your computer and use it in GitHub Desktop.
Save thinktainer/6400082 to your computer and use it in GitHub Desktop.
Chris Patterson's IDisposable implementation from: http://lostechies.com/chrispatterson/2012/11/29/idisposable-done-right/
public class DisposableClass :
IDisposable
{
bool _disposed;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~DisposableClass()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;
if (disposing)
{
// free other managed objects that implement
// IDisposable only
}
// release any unmanaged objects
// set the object references to null
_disposed = true;
}
}
public class SubDisposableClass :
DisposableClass
{
private bool _disposed;
// a finalizer is not necessary, as it is inherited from
// the base class
protected override void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
// free other managed objects that implement
// IDisposable only
}
// release any unmanaged objects
// set object references to null
_disposed = true;
}
base.Dispose(disposing);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment