Created
June 24, 2015 20:51
-
-
Save qbit86/8a944c9eb5d183d7d45a to your computer and use it in GitHub Desktop.
Disposable Pattern
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 /*non-sealed*/ class Foo : IDisposable | |
{ | |
public /*implicitly sealed*/ void Dispose() | |
{ | |
Console.WriteLine("Disposing Foo managed resources."); | |
DisposeFooCore(); | |
} | |
protected virtual void DisposeFooCore() { } | |
} | |
public /*non-sealed*/ class Bar : Foo | |
{ | |
protected sealed override void DisposeFooCore() | |
{ | |
Console.WriteLine("Disposing Bar managed resources."); | |
DisposeBarCore(); | |
} | |
protected virtual void DisposeBarCore() { } | |
} | |
public sealed class Qux : Bar | |
{ | |
protected override void DisposeBarCore() | |
{ | |
Console.WriteLine("Disposing Qux managed resources."); | |
} | |
} | |
... | |
using (new Qux()) { } | |
// Output: | |
// Disposing Foo managed resources. | |
// Disposing Bar managed resources. | |
// Disposing Qux managed resources. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment