Bem.Collections.WeakDictionary 1.0.1

dotnet add package Bem.Collections.WeakDictionary --version 1.0.1
NuGet\Install-Package Bem.Collections.WeakDictionary -Version 1.0.1
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="Bem.Collections.WeakDictionary" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Bem.Collections.WeakDictionary --version 1.0.1
#r "nuget: Bem.Collections.WeakDictionary, 1.0.1"
#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 Bem.Collections.WeakDictionary as a Cake Addin
#addin nuget:?package=Bem.Collections.WeakDictionary&version=1.0.1

// Install Bem.Collections.WeakDictionary as a Cake Tool
#tool nuget:?package=Bem.Collections.WeakDictionary&version=1.0.1

Bem.Collections.WeakDictionary

Nuget (with prereleases) GitHub release (latest by date) Azure DevOps builds Azure DevOps tests Azure DevOps coverage

Overview

A thread-safe dictionary implementation that stores values with weekly referenced keys. See WeakReference<T>.
Once a key is not strong-referenced anymore the entry will be removed automatically from WeakDictionary<TKey,TValue>.
This collection can be used for similar purposes as ConditionalWeakTable<TKey,TValue> except that WeakDictionary<TKey,TValue> can accept custom IEqualityComparer<T> instance for key comparison. ConditionalWeakTable<TKey,TValue> can only compare keys by reference no matter if GetHashCode() and Equals() methods are overridden or not.
Internally WeakDictionary<TKey,TValue> uses a combination of Dictionary<TKey,TValue> and ConditionalWeakTable<TKey,TValue> types to manage entries.

Usage

Use the same way as a generic Dictionary<TKey,TValue>.

var dictionary = new WeakDictionary<MyReferenceType, AnyType>(optionalEqualityComparer);

Note that WeakDictionary<TKey,TValue> implements only IDictionary<TKey,TValue>, IEnumerable<T> and IEnumerable interfaces.
Note that TKey must be reference type, but TValue can be anything you want.
Also note that TValue by default is not stored as a WeakReference<T>. Therefore if key and value reference the same object GC can not collect the instance!
Consider this if you plan to use WeakDictionary<TKey,TValue> as a HashSet<T> and avoid using the same instance for key and value.

Use Cases

The single use case of this library is when you need to use a custom IEqualityComparer<T> for the weakly referenced keys. If this is not a requirement use the ConditionalWeakTable<TKey,Tvalue> instead.

More info on GitHub

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • 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
1.0.1 233 11/3/2023
1.0.0 104 11/3/2023