Skip to content

Instantly share code, notes, and snippets.

@guojuntech
Created January 20, 2014 14:30
Show Gist options
  • Select an option

  • Save guojuntech/8520819 to your computer and use it in GitHub Desktop.

Select an option

Save guojuntech/8520819 to your computer and use it in GitHub Desktop.
IDisposable pattern. 1. Only use SuppressFinalize when class has unmanaged resources as their member directly. Class has finalizers will has it's position on GC's finalize queue.
public class MyClass : IDisposable
{
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// called via myClass.Dispose().
// OK to use any private object references
}
disposed = true;
}
}
public void Dispose() // Implement IDisposable
{
Dispose(true);
GC.SuppressFinalize(this);
}
~MyClass() // the finalizer
{
Dispose(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment