Skip to content

Instantly share code, notes, and snippets.

@sandord
Last active December 1, 2022 14:49
Show Gist options
  • Save sandord/400557 to your computer and use it in GitHub Desktop.
Save sandord/400557 to your computer and use it in GitHub Desktop.
Disposable pattern
public class MyClass : IDisposable
{
/// <summary>
/// Finalizes an instance of the <see cref="MyClass"/> class. Releases unmanaged
/// resources and performs other cleanup operations before the current instance is
/// reclaimed by garbage collection.
/// </summary>
~MyClass()
{
this.Dispose(false);
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting
/// unmanaged resources.
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// Release managed resources.
}
// Release unmanaged resources.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment