Dispose Finalize Pattern

// // As Described on this webpage: // http://www.codeproject.com/Articles/15360/Implementing-IDisposable-and-the-Dispose-Pattern-P // public class MyDisposableClass : System.IDisposable { #region IDisposable Implementation private bool _isDisposed = false; /// <summary> /// Releases all resource used by the <see cref="MyDisposableClass"/> object. /// </summary> /// <remarks>Call <see cref="Dispose"/> when you are finished using the <see cref="MyDisposableClass"/>. The /// <see cref="Dispose"/> method leaves the <see cref="MyDisposableClass"/> in an unusable state. After calling /// <see cref="Dispose"/>, you must release all references to the <see cref="MyDisposableClass"/> so the garbage /// collector can reclaim the memory that the <see cref="MyDisposableClass"/> was occupying.</remarks> public void Dispose () { Dispose (true); System.GC.SuppressFinalize (this); } protected virtual void Dispose(bool disposing) { if(!_isDisposed) { if(disposing) { //managed// //dispose-only, i.e. non-finalizable logic// } //unmanaged// _isDisposed = true; } } ~MyDisposableClass() { Dispose(false); } #endregion }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.