Skip to content

Instantly share code, notes, and snippets.

@Dynyx
Created June 4, 2012 13:06
Show Gist options
  • Save Dynyx/2868253 to your computer and use it in GitHub Desktop.
Save Dynyx/2868253 to your computer and use it in GitHub Desktop.
IDisposable class example
public class ClassName : IDisposable
{
#region Variable Declarations
private bool _disposed = false;
#endregion
#region Constructors & Destructors
public ClassName()
{
}
~ClassName()
{
//Invoke the helper class that will clean up and release any applicable resources.
//Note that we are passing a false which indicates that the Garbage Collector
//triggered the clean up.
ReleaseResources(false);
}
#endregion
#region IDisposable Interface Methods
public void Dispose()
{
//Invoke the helper class that will clean up and release any applicable resources.
//Note that we are passing a true which indicates that the method was invoked by
//the caller/user.
ReleaseResources(true);
//Suppress the Finalization step of the Garbage Collector.
GC.SuppressFinalize(this);
}
private void ReleaseResources(bool disposing)
{
//Ensure that the object has not already been disposed.
if (!this._disposed)
{
if (disposing)
{
//Clean up and dispose of all MANAGED resources here.
}
//Clean up and dispose of all UNMANAGED resources here.
}
//Indicate that the object has already been disposed.
this._disposed = true;
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment