Skip to content

Instantly share code, notes, and snippets.

@gustavodaquino
Created May 8, 2017 04:37
Show Gist options
  • Save gustavodaquino/516b1b22ef4c1313ad1da9f5384b63af to your computer and use it in GitHub Desktop.
Save gustavodaquino/516b1b22ef4c1313ad1da9f5384b63af to your computer and use it in GitHub Desktop.
Basic Dispose Pattern in C#
public class DisposableResourceHolder : IDisposable {
private SafeHandle resource; // handle to a resource
public DisposableResourceHolder(){
this.resource = ... // allocates the resource
}
public void Dispose(){
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing){
if (disposing){
if (resource!= null) resource.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment