Skip to content

Instantly share code, notes, and snippets.

@alistairjevans
Last active October 24, 2019 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alistairjevans/49f0cc0542d1c667b5d797fcaabe4fe3 to your computer and use it in GitHub Desktop.
Save alistairjevans/49f0cc0542d1c667b5d797fcaabe4fe3 to your computer and use it in GitHub Desktop.
Dispose once only
private class MyDisposableObject : IDisposable, IAsyncDisposable
{
private SqlConnection myResource = new SqlConnection();
private bool isDisposed = false;
public void Dispose()
{
if (!isDisposed)
{
isDisposed = true;
myResource.Dispose();
}
}
public async ValueTask DisposeAsync()
{
if (!isDisposed)
{
// Set isDisposed to true first
isDisposed = true;
await myResource.DisposeAsync();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment