Last active
December 26, 2015 05:39
-
-
Save Rcomian/7101725 to your computer and use it in GitHub Desktop.
IDisposable
More details here: http://www.codeproject.com/Articles/15360/Implementing-IDisposable-and-the-Dispose-Pattern-P
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
class MyClass : IDisposable | |
{ | |
private bool disposed = false; | |
protected virtual void Dispose(bool disposing) | |
{ | |
if (!disposed) | |
{ | |
if (disposing) | |
{ | |
// Dispose managed resources | |
} | |
// Dispose unmanaged resources | |
disposed = true; | |
} | |
} | |
~MyClass() | |
{ | |
Dispose(false); | |
} | |
public void Dispose() | |
{ | |
Dispose(true); | |
GC.SuppressFinalize(this); | |
} | |
} |
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
class MyDerivedClass : MyClass | |
{ | |
private bool disposed = false; // to detect redundant calls | |
public MyDerivedClass() | |
:base() | |
{ | |
} | |
protected override void Dispose(bool disposing) | |
{ | |
if (!disposed) | |
{ | |
if (disposing) | |
{ | |
// Dispose this class's managed resources | |
} | |
// Dispose this class's non-managed resources | |
disposed = true; | |
} | |
base.Dispose(disposing); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment