I created this class to make life a little bit easier for me.
You are free to use it as you wish!
How to use:
Override this class, override ReleaseManaged() and ReleaseUnmanaged() with the appropriate code, and you are good to go.
namespace SuperDisposeImplementation { using System; /// <summary> /// Override this class for easy releasing of managed and unmanaged code. /// </summary> /// <remarks> /// By Kristof Mattei /// Use as you wish /// I don't hold the copyright /// Combined from code I found everywhere. /// </remarks> public abstract class SuperDispose : IDisposable { /// <summary> /// True if managed resources are already cleaned up, false if not /// </summary> private bool _disposed; #region IDisposable Members /// <summary> /// Implementation of IDisposable.Dispose(). Don't make virtual /// </summary> public void Dispose() { this.Dispose(true); // This object will be cleaned up by the Dispose method. // Therefore, you should call GC.SupressFinalize to // take this object off the finalization queue // and prevent finalization code for this object // from executing a second time. GC.SuppressFinalize(this); } #endregion /// <summary> /// Dispose(bool disposing) executes in two distinct scenarios. /// If disposing equals true, the method has been called directly /// or indirectly by a user's code. Managed and unmanaged resources /// can be disposed. /// If disposing equals false, the method has been called by the /// runtime from inside the finalizer and you should not reference /// other objects. Only unmanaged resources can be disposed. /// </summary> /// <param name="disposing">True when called from the Dispose, false when called from the ~. Don't call yourself</param> private void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (!this._disposed) { // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { this.ReleaseManaged(); } // Dispose unmanaged resources. this.ReleaseUnmanaged(); // disposing has been done, make sure we don't dispose the managed ones again. this._disposed = true; } } /// <summary> /// Override this method, and release unmanaged resources in that method /// </summary> protected abstract void ReleaseUnmanaged(); /// <summary> /// Override this method, and release managed resources in that method /// </summary> protected abstract void ReleaseManaged(); /// <summary> /// Use C# destructor syntax for finalization code. /// This destructor will run only if the Dispose method /// does not get called. /// It gives your base class the opportunity to finalize. /// Do not provide destructors in types derived from this class. /// </summary> ~SuperDispose() { // make sure we don't dispose managed resources, hence the false // this is because we can't control the called order of this.Dispose(false); } } }