Skip to content

Instantly share code, notes, and snippets.

@xiaomi7732
Created January 21, 2022 06:21
Show Gist options
  • Save xiaomi7732/146dd6ea68483b7215e336ca4783d4e6 to your computer and use it in GitHub Desktop.
Save xiaomi7732/146dd6ea68483b7215e336ca4783d4e6 to your computer and use it in GitHub Desktop.
Hidden Disposables - Refresh

Hidden Disposables

I didn't know there's something like this. And I found it here:

https://docs.microsoft.com/en-us/archive/msdn-magazine/2016/november/net-framework-hidden-disposables

This is a refresh of the implementations discussed.

The implementations is still valid but aged (so will this one), and I gave it a refresh.

TL;DR

This is useful in case you have a Factory that returns an abstraction of an object by interface. The interface doesn't implement IDisposable since there's no enforce for implementation to have one, for example:

  • interface ISomething;
  • class Paper : ISomething;
  • class Phone : ISomething, IDisposable;

And factory:

  • class ItemFactory that creates ISomething (in this implementation, it depends on whether the current second is a even or odd number).

There are several solutions, refer to Program.cs for details. But basically:

  • Let the caller decide - that will become tedious;
  • Provide an extension method - I came this up;
  • Use a wrapper class of PotentialDisposable;

Have fun!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment