A quick post to show the implementation of the Dispose pattern for IDisposable implementations.
Note: this is not threadsafe
public class MyObject : IDisposable
{
protected bool disposed;
// only required if we need to release unmanaged resources
~MyObject()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if(!disposed)
{
if(disposing)
{
// dispose of any managed resources here
}
// clean up any unmanaged resources here
}
disposed = true;
}
}