NCode.Disposables 7.1.0

Prefix Reserved
dotnet add package NCode.Disposables --version 7.1.0
                    
NuGet\Install-Package NCode.Disposables -Version 7.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NCode.Disposables" Version="7.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NCode.Disposables" Version="7.1.0" />
                    
Directory.Packages.props
<PackageReference Include="NCode.Disposables" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NCode.Disposables --version 7.1.0
                    
#r "nuget: NCode.Disposables, 7.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package NCode.Disposables@7.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NCode.Disposables&version=7.1.0
                    
Install as a Cake Addin
#tool nuget:?package=NCode.Disposables&version=7.1.0
                    
Install as a Cake Tool

NCode.Disposables

CI Target framework License

Building blocks for IDisposable and IAsyncDisposable in .NET: run an action on dispose, dispose many resources in reverse order, hold a swappable, serial or single-assignment resource, guard a resource until ownership moves on, cancel on dispose, share a resource through reference-counted leases, scope a lock, and dispose on a synchronization context.

Installation

dotnet add package NCode.Disposables

The package depends on no other package. Its target frameworks are the ones its .csproj declares; they are listed on nuget.org's Frameworks tab and shown by the badge above. CHANGELOG.md records when a framework is added or dropped.

Example

Every type is in the NCode.Disposables namespace. Disposable.Create turns a delegate into an IDisposable, and Disposable.Collection disposes many of them together, the last one added first:

var log = new List<string>();
var connection = Disposable.Create(() => log.Add("connection closed"));
var fileLock = Disposable.Create(() => log.Add("lock released"));

using (var resources = Disposable.Collection())
{
    resources.Add(connection);
    resources.Add(fileLock);
}

// log: "lock released", "connection closed": the last resource added is disposed first

By default the action runs on the first Dispose only; Disposable.CreateRepeatable runs it on every call:

var count = 0;

var once = Disposable.Create(() => count++);
once.Dispose();
once.Dispose(); // count == 1: the action runs on the first call only

var everyTime = Disposable.CreateRepeatable(() => count++);
everyTime.Dispose();
everyTime.Dispose(); // count == 3: the action runs on every call

SharedReference.Create puts a resource behind a reference count, and the resource is disposed when the last lease is disposed:

var log = new List<string>();
var connection = Disposable.Create(() => log.Add("connection closed"));

var first = SharedReference.Create(connection);
var second = first.AddReference();

first.Dispose(); // log is empty: second still holds a reference
second.Dispose(); // log: "connection closed"

Learn more

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.

NuGet packages (25)

Showing the top 5 NuGet packages that depend on NCode.Disposables:

Package Downloads
NCode.Collections.Providers

Default implementations for dynamic, change-aware collection providers. Includes static, observable, composite, and periodic polling data sources with automatic change notifications via IChangeToken. Provides dependency injection integration with Microsoft.Extensions.DependencyInjection.

NCode.Identity.OpenId.Abstractions

Package Description

NCode.Identity.OpenId.Accounts.Abstractions

Package Description

NCode.Identity.OpenId.Authentication.Abstractions

Package Description

NCode.Identity.Secrets

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
7.1.0 0 11/25/2026
7.0.0 0 11/25/2026
6.0.0 0 11/24/2026
5.3.0 4,085 1/17/2026
5.2.1 255 11/29/2024
5.1.1 234 11/27/2024
5.1.0 250 5/16/2024
5.0.2 261 5/16/2024
5.0.1 262 5/16/2024
5.0.0 239 5/16/2024
4.4.0 260 5/15/2024
4.3.0 248 5/15/2024
4.2.0 240 4/29/2024
4.1.0 244 4/28/2024
4.0.0 240 4/21/2024
3.1.0 234 4/21/2024
3.0.1 244 4/21/2024
3.0.0 252 4/21/2024
2.0.2 1,935 1/22/2017
2.0.1 1,364 1/22/2017
Loading failed