NCode.Disposables 4.0.0

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
dotnet add package NCode.Disposables --version 4.0.0
NuGet\Install-Package NCode.Disposables -Version 4.0.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="4.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NCode.Disposables --version 4.0.0
#r "nuget: NCode.Disposables, 4.0.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.
// Install NCode.Disposables as a Cake Addin
#addin nuget:?package=NCode.Disposables&version=4.0.0

// Install NCode.Disposables as a Cake Tool
#tool nuget:?package=NCode.Disposables&version=4.0.0

ci Nuget

NCode.Disposables

This library provides a set of useful IDisposable implementations.

Disposable Empty

Provides an implementation of IDisposable that is empty and performs nothing (i.e. nop) when Dispose is called.

void Example()
{
    // using singleton instance:
    IDisposable disposable1 = Disposable.Empty;
    disposable1.Dispose();
    
    // using new instance:
    IDisposable disposable2 = new DisposableEmpty();
    disposable2.Dispose();
}

Disposable Action

Provides an IDisposable implementation that will invoke an Action delegate when Dispose is called. If the Dispose method is called multiple times, the underlying action is invoked only once on the first call.

void Example()
{
    IDisposable disposable = Disposable.Create(() =>
        Console.WriteLine("I am disposed."));
    // ...
    disposable.Dispose();
}

Disposable Aggregate

Provides an IDisposable implementation that contains (i.e. aggregates) a property to another underlying IDisposable resource. The underlying resource may be assigned or retrieved multiple times as long as the aggregate hasn't been disposed yet.

void Example()
{
    IDisposable resource = CreateSomeResource();
    // ...
    var aggregate = Disposable.Aggregate(resource);
    // ...
    if (SomeCondition) {
        var previous = aggregate.Disposable;
        // ...
        aggregate.Disposable = CreateSomeOtherResource();
    }
    aggregate.Dispose();
    // ...
}

Disposable Collection

Provides an IDisposable collection that contains other IDisposable resources that will be disposed when the collection itself is disposed. The items in the collection are disposed in reverse order that they are added. The items in the collection may be added, removed, or cleared at any time before the collection is disposed itself.

void Example()
{
    IDisposable resource1 = CreateResource1();
    IDisposable resource2 = CreateResource2();
    // ...
    var collection = Disposable.Collection(resource1, resource2);
    // ...
    var resource3 = CreateResource3();
    if (!collection.Contains(resource3))
        collection.Add(resource3);
    // ...
    collection.Dispose();
}

Disposable Reference Count

Provides an IDisposable implementation that uses reference counting and only disposes the underlying resource when all the references have been released (i.e. reference count is zero). Calling the Dispose method is idempotent safe and calling it multiple times has the same effect as calling it only once.

void Example()
{
    IDisposable resource = CreateResource();
    // ...
    var first = Disposable.Shared(resource);
    var second = first.AddReference();
    var third = second.AddReference();
    // ...
    first.Dispose();
    second.Dispose();
    third.Dispose();
    // the resource will be disposed here after
    // all 3 references have been disposed...
}

Disposable Context

Provides an IDisposable implementation that will invoke the Dispose method of an underlying resource using an asynchronous or synchronous operation from a SynchronizationContext. If the Dispose method is called multiple times, the underlying resource is only disposed once on the first call.

void Example()
{
    IDisposable resource = CreateResource();
    // ...
    const bool async = true;
    var context = SynchronizationContext.Current;
    var disposable = Disposable.Context(resource, context, async);
    // ...
    disposable.Dispose()
}

Feedback

Please provide any feedback, comments, or issues to this GitHub project here.

Release Notes

  • v1.0.0 - Initial release
  • v1.0.1 - Unknown changes
  • v2.0.1 - Unknown changes
  • v2.0.2 - Port to .NET Core/Standard
  • v3.0.0 - Port to .NET 8.0 and refactor shared reference implementation
  • v3.0.1 - Updated xml documentation
  • v3.1.0 - Split ISharedReference into ISharedReferenceScope and ISharedReferenceProvider
  • v4.0.0 - Revert the split
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.0.0 83 4/21/2024
3.1.0 69 4/21/2024
3.0.1 80 4/21/2024
3.0.0 78 4/21/2024
2.0.2 1,175 1/22/2017
2.0.1 935 1/22/2017
1.0.1 1,262 2/11/2016
1.0.0 1,184 1/30/2016

Built on 2024-04-21 14:25:09Z