#readwise
# ConditionalWeakTable<TKey,TValue> Class

## Metadata
- Author: [[docs.microsoft.com]]
- Full Title: ConditionalWeakTable<TKey,TValue> Class
- URL: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.conditionalweaktable-2
## Highlights
- The ConditionalWeakTable<TKey,TValue> class enables language compilers to attach arbitrary properties to managed objects at run time. A ConditionalWeakTable<TKey,TValue> object is a dictionary that binds a managed object, which is represented by a key, to its attached property, which is represented by a value. The object's keys are the individual instances of the TKey class to which the property is attached, and its values are the property values that are assigned to the corresponding objects.
Keys must be unique; in other words, the ConditionalWeakTable<TKey,TValue> class supports one attached value per managed object. Two keys are equal if passing them to the Object.ReferenceEquals method returns true.
- Although the ConditionalWeakTable<TKey,TValue> class holds a collection of key/value pairs, it is best thought of as a table rather than a dictionary object. The ConditionalWeakTable<TKey,TValue> class differs from a dictionary in several ways:
It does not persist keys. That is, a key is not kept alive only because it is a member of the collection.
It does not include all the methods (such as GetEnumerator or Contains) that a dictionary typically has.
It does not implement the IDictionary<TKey,TValue> interface.
- The ConditionalWeakTable<TKey,TValue> class differs from other collection objects in its management of the object lifetime of keys stored in the collection. Ordinarily, when an object is stored in a collection, its lifetime lasts until it is removed (and there are no additional references to the object) or until the collection object itself is destroyed. However, in the ConditionalWeakTable<TKey,TValue> class, adding a key/value pair to the table does not ensure that the key will persist, even if it can be reached directly from a value stored in the table (for example, if the table contains one key, A, with a value V1, and a second key, B, with a value P2 that contains a reference to A). Instead, ConditionalWeakTable<TKey,TValue> automatically removes the key/value entry as soon as no other references to a key exist outside the table.
- Instances of the ConditionalWeakTable<TKey,TValue> class are thread safe. They do not require callers to do any additional locking.