Skip to content
The quick start's collection as it is disposed: the last resource added goes first, and each goes once. A drawing of the order, not a trace of a real process.

Clean up in the right order, once.

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.

Terminal window
dotnet add package NCode.Disposables

NuGet version NuGet downloads CI Target framework OpenSSF Scorecard License

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

  • Run each class’s cleanup once, even when several threads dispose it, unless a member named Repeatable asks for every call.
  • Dispose a collection’s items in reverse order, every one even when one throws.
  • Dispose a resource handed to a holder that is already disposed. ADR-1004
  • Share one resource through reference-counted leases.
  • Dispose a resource on its SynchronizationContext.
  • Scope a SemaphoreSlim or ReaderWriterLockSlim lock in a struct that allocates nothing.

It does not

  • Make a struct safe to dispose twice. A value disposable, a lock scope or a lease runs again on every dispose. Once, or on every call
  • Dispose an item that is only IAsyncDisposable from synchronous code: DisposeAll() throws InvalidOperationException.
  • Dispose anything you never dispose. No type has a finalizer.
  • Hide failures. The exceptions items throw reach you unless you choose a member named IgnoringExceptions.
  • Dispose a resource on the calling thread when its context refuses the work.

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.

  • One assembly, 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.
  • The target framework is the one the project file declares, which the badge shows and nuget.org lists.
  • Every C# sample on this site is a test the pull request build compiles and runs.
  • The reasons behind the design are on record in docs/decisions.
  • Apache License 2.0, copyright NCode Group. A vulnerability is reported privately, as the security policy says.