Created
January 20, 2014 14:30
-
-
Save guojuntech/8520819 to your computer and use it in GitHub Desktop.
IDisposable pattern.
1. Only use SuppressFinalize when class has unmanaged resources as their member directly.
Class has finalizers will has it's position on GC's finalize queue.
This file contains hidden or 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
| public class MyClass : IDisposable | |
| { | |
| private bool disposed = false; | |
| protected virtual void Dispose(bool disposing) | |
| { | |
| if (!disposed) | |
| { | |
| if (disposing) | |
| { | |
| // called via myClass.Dispose(). | |
| // OK to use any private object references | |
| } | |
| disposed = true; | |
| } | |
| } | |
| public void Dispose() // Implement IDisposable | |
| { | |
| Dispose(true); | |
| GC.SuppressFinalize(this); | |
| } | |
| ~MyClass() // the finalizer | |
| { | |
| Dispose(false); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment