ConditionalWeakTable(TKey, TValue) Class
Enables compilers to dynamically attach object fields to managed objects.
Assembly: mscorlib (in mscorlib.dll)
Thetype exposes the following members.
|Add||Adds a key to the table.|
|Equals(Object)||Determines whether the specified Object is equal to the current Object. (Inherited from Object.)|
|Finalize||Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)|
|GetHashCode||Serves as a hash function for a particular type. (Inherited from Object.)|
|GetOrCreateValue||Atomically searches for a specified key in the table and returns the corresponding value. If the key does not exist in the table, the method invokes the default constructor of the class that represents the table's value to create a value that is bound to the specified key.|
|GetType||Gets the Type of the current instance. (Inherited from Object.)|
|GetValue||Atomically searches for a specified key in the table and returns the corresponding value. If the key does not exist in the table, the method invokes a callback method to create a value that is bound to the specified key.|
|MemberwiseClone||Creates a shallow copy of the current Object. (Inherited from Object.)|
|Remove||Removes a key and its value from the table.|
|ToString||Returns a string that represents the current object. (Inherited from Object.)|
|TryGetValue||Gets the value of the specified key.|
The class enables language compilers to attach arbitrary properties to managed objects at run time. A 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 class supports one attached value per managed object. Two keys are equal if passing them to the Object.ReferenceEquals method returns true.
You cannot control equality comparisons by overriding Object.GetHashCode to explicitly set the hash code for a key. The class does not use the Object.GetHashCode method to compute hash codes, and therefore does not invoke Object.GetHashCode overrides.
Although the class holds a collection of key/value pairs, it is best thought of as a table rather than a dictionary object. The 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 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 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, automatically removes the key/value entry as soon as no other references to a key exist outside the table. The example provides an illustration.
The following example illustrates that a key stored in the table does not persist after references to it outside the table are destroyed. The example defines two classes: ManagedClass, which represents the key in the table, and AttachedProperty, which represents the key's value. The example instantiates three objects of each type. It also instantiates a WeakReference object that represents the second ManagedClass, and then destroys the second ManagedClass instance. The attempt to retrieve the second ManagedClass object from the Target property indicates that no references to the object remain.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.