Class WeakValueDictionary<TKey, TValue>
Represents a collection of keys and weakly referenced values that automatically removes entries as their values are garbage collected.
Namespace: Singulink.Collections
Assembly: Singulink.Collections.Weak.dll
Syntax
public sealed class WeakValueDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable, IDisposable where TKey : notnull where TValue : class
Type Parameters
| Name | Description |
|---|---|
| TKey | |
| TValue |
Remarks
Values are held through weak references, so adding a value to the dictionary does not prevent it from being garbage collected. When a value is collected its entry is removed from the dictionary automatically on the finalizer thread shortly afterwards, so the dictionary never needs to be cleaned and lookups and enumeration never return null values.
Individual operations are safe for concurrent use from multiple threads without external synchronization, including while other threads are enumerating the dictionary. Compound operations that must observe a consistent view of the dictionary still require external synchronization.
Key comparers that throw from Equals(T, T) or GetHashCode(T) are not supported and may leave the dictionary unusable after the exception.
When a value dies, its entry lingers until the finalizer thread removes it. If an equal key is added during that window the entry is replaced in place and the dictionary keeps the key instance it already had, so Keys and enumeration return the original key instance rather than the one passed to the later add. This matches the key retention behavior of Dictionary<TKey, TValue> and is only observable when equal keys are distinguishable, such as by reference. Callers that require the new key instance to be stored can synchronize access and call Remove(TKey) before adding.
Dispose dictionaries that are no longer needed instead of letting them be finalized. The finalizer removes all remaining entries, which can block the finalizer thread for a significant amount of time if the dictionary is large.
Constructors
| Name | Description |
|---|---|
| WeakValueDictionary() | Initializes a new instance of the WeakValueDictionary<TKey, TValue> class. |
| WeakValueDictionary(IEqualityComparer<TKey>?) | Initializes a new instance of the WeakValueDictionary<TKey, TValue> class that uses the specified key comparer, or the default comparer if null is specified. |
Properties
| Name | Description |
|---|---|
| Comparer | Gets the comparer that is used to determine equality of the keys. |
| Count | Gets the number of entries in the dictionary. |
| this[TKey] | Gets or sets the value associated with the specified key. Setting a value replaces any existing value for the key. |
| Keys | Gets the keys in the dictionary whose values are still available. |
| Values | Gets the values in the dictionary that are still available. |
Methods
| Name | Description |
|---|---|
| Add(TKey, TValue) | Adds the specified key and value to the dictionary. |
| Clear() | Removes all entries from the dictionary. |
| Contains(KeyValuePair<TKey, TValue>, IEqualityComparer<TValue>?) | Determines whether the dictionary contains the specified key and value pair. |
| Contains(TKey, TValue, IEqualityComparer<TValue>?) | Determines whether the dictionary contains the specified key with a value equal to the specified value. |
| ContainsKey(TKey) | Determines whether the dictionary contains the specified key with a value that is still available. |
| ContainsValue(TValue, IEqualityComparer<TValue>?) | Determines whether the dictionary contains a value equal to the specified value. |
| Dispose() | Removes all entries from the dictionary and prevents further use. Most members throw ObjectDisposedException afterwards. |
| ~WeakValueDictionary() | Finalizes an instance of the WeakValueDictionary<TKey, TValue> class. |
| GetAlternateLookup<TAlternateKey>() | Gets an alternate lookup that can be used to perform operations on this dictionary using |
| GetEnumerator() | Returns an enumerator that iterates through the entries in the dictionary whose values are still available. |
| Remove(TKey) | Removes the entry with the specified key from the dictionary. |
| Remove(TKey, TValue, IEqualityComparer<TValue>?) | Removes the entry with the specified key from the dictionary if its value is equal to the specified value. |
| Remove(TKey, out TValue) | Removes the entry with the specified key from the dictionary. |
| TryAdd(TKey, TValue) | Attempts to add the specified key and value to the dictionary. |
| TryGetAlternateLookup<TAlternateKey>(out AlternateLookup<TAlternateKey>) | Attempts to get an alternate lookup that can be used to perform operations on this dictionary using |
| TryGetValue(TKey, out TValue?) | Gets the value associated with the specified key. |
Explicit Interface Implementations
| Name | Description |
|---|---|
| IEnumerable.GetEnumerator() | Returns an enumerator that iterates through the key/value pairs in the dictionary. |