NCode.Disposables 4.1.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package NCode.Disposables --version 4.1.0                
NuGet\Install-Package NCode.Disposables -Version 4.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="4.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NCode.Disposables --version 4.1.0                
#r "nuget: NCode.Disposables, 4.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.
// Install NCode.Disposables as a Cake Addin
#addin nuget:?package=NCode.Disposables&version=4.1.0

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

ci Nuget

NCode.Disposables

This library provides a set of useful IDisposable and IAsyncDisposable implementations. For brevity, the IDisposable interface is used in the examples below, but the same implementations are available for IAsyncDisposable as well.

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
  • v4.1.0 - Added async support
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
5.1.0 121 5/16/2024
5.0.2 114 5/16/2024
5.0.1 119 5/16/2024
5.0.0 114 5/16/2024
4.4.0 100 5/15/2024
4.3.0 121 5/15/2024
4.2.0 115 4/29/2024
4.1.0 107 4/28/2024
4.0.0 105 4/21/2024
3.1.0 108 4/21/2024
3.0.1 116 4/21/2024
3.0.0 112 4/21/2024
2.0.2 1,231 1/22/2017
2.0.1 947 1/22/2017
1.0.1 1,289 2/11/2016
1.0.0 1,199 1/30/2016

Built on 2024-04-28 23:43:29Z