You have a task
Each guide is one situation, its code and its pitfalls.
One using statement disposes one thing. Real cleanup undoes a registration, disposes several resources in the right order, hands one resource to several owners or waits for a UI thread. NCode.Disposables gives each of these a small type, and every sample on this site is a test.
It depends on no other package, does no I/O and starts no threads.
dotnet add package NCode.DisposablesYou have a task
Each guide is one situation, its code and its pitfalls.
You are choosing a type
You want the rules
Three resources go into one collection, and one Dispose releases all of them.
List<string> log = [];
var connection = Disposable.Create(() => log.Add("connection closed"));var cache = Disposable.Create(() => log.Add("cache flushed"));var fileLock = Disposable.Create(() => log.Add("lock released"));
using (var resources = Disposable.Collection(connection, cache, fileLock)){ // work with the three resources}
// log: "lock released", "cache flushed", "connection closed"The drawing above is this sample; hold a resource that changes shows what each holder does with the resources it is given.
It does
Repeatable asks for
every call.SynchronizationContext.SemaphoreSlim or ReaderWriterLockSlim lock in a struct that allocates nothing.It does not
IAsyncDisposable from synchronous code: DisposeAll() throws
InvalidOperationException.IgnoringExceptions.Each guide is one situation, its code and its pitfalls.
Not sure which type you need? Which disposable? asks a few questions and names one.
NCode.Disposables, with every type in the namespace NCode.Disposables. It has no dependencies,
performs no I/O, starts no threads and keeps no mutable static state.