Skip to content

Instantly share code, notes, and snippets.

@rbwestmoreland
Created December 13, 2011 01:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbwestmoreland/1470081 to your computer and use it in GitHub Desktop.
Save rbwestmoreland/1470081 to your computer and use it in GitHub Desktop.
Properly Implementing the Dispose Pattern
using System;
/// <summary>
/// An example Dispose pattern.
/// <para>The Dispose pattern is a design pattern, whose
/// purpose is to handle resource cleanup in runtime
/// environments that use automatic garbage collection.</para>
/// </summary>
public class YourClass : IDisposable
{
#region Disposable Member(s)
private bool Disposed { get; set; }
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!Disposed)
{
if (disposing)
{
// Free other state (managed objects).
}
// Free your own state (unmanaged objects).
// Set large fields to null.
Disposed = true;
}
}
~YourClass()
{
Dispose(false);
}
#endregion Disposable Member(s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment