Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.