Async Disposables The Easy Way

1 · Phil Haack · Dec. 10, 2021, midnight
In the System.Reactive.Disposables namespace (part of Reactive Extensions), there’s a small and useful Disposable class. It has a Create method that takes in an Action and returns an IDisposable instance. When that instance is disposed, the action is called. It’s a nice way of creating an ad-hoc IDisposable. I use them often for creating a scope in code where something should happen at the end of the scope. Here’s an exceedingly trivial example: Console.WriteLine("Working on it..."); using var s...