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
<PackageReference Include="Bem.Collections.WeakDictionary" Version="1.0.1" />
paket add Bem.Collections.WeakDictionary --version 1.0.1
#r "nuget: Bem.Collections.WeakDictionary, 1.0.1"
// 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
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.
Product | Versions 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.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. |
-
.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.