Created
December 13, 2011 01:58
-
-
Save rbwestmoreland/1470081 to your computer and use it in GitHub Desktop.
Properly Implementing the Dispose Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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