Skip to content

Instantly share code, notes, and snippets.

@qbit86
Created June 24, 2015 20:51
Show Gist options
  • Save qbit86/8a944c9eb5d183d7d45a to your computer and use it in GitHub Desktop.
Save qbit86/8a944c9eb5d183d7d45a to your computer and use it in GitHub Desktop.
Disposable Pattern
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