Created
October 24, 2019 14:30
-
-
Save alistairjevans/4e1f920bab18aa3104b67ca70e64f22d to your computer and use it in GitHub Desktop.
Call GC.SuppressFinalize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private class MyDisposableObject : IDisposable, IAsyncDisposable | |
{ | |
private SqlConnection myResource = new SqlConnection(); | |
private bool isDisposed = false; | |
public void Dispose() | |
{ | |
if (!isDisposed) | |
{ | |
isDisposed = true; | |
Dispose(true); | |
GC.SuppressFinalize(this); | |
} | |
} | |
protected virtual void Dispose(bool disposing) | |
{ | |
if(disposing) | |
{ | |
myResource.Dispose(); | |
} | |
} | |
public async ValueTask DisposeAsync() | |
{ | |
if (!isDisposed) | |
{ | |
isDisposed = true; | |
await DisposeAsync(true); | |
GC.SuppressFinalize(this); | |
} | |
} | |
protected async ValueTask DisposeAsync(bool disposing) | |
{ | |
if (disposing) | |
{ | |
await myResource.DisposeAsync(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment