ConditionalWeakTable<TKey, TValue>.Add Method
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Adds a key to the table.
Assembly: mscorlib (in mscorlib.dll)
| Exception | Condition |
|---|---|
| ArgumentNullException | key is null. |
| ArgumentException | key already exists. |
Every key in a ConditionalWeakTable<TKey, TValue> object must be unique. Keys are not unique if they refer to the same object (that is, if passing them as arguments to the Object.ReferenceEquals method returns true).
If the key is garbage-collected during the addition operation, the existing key/value pair is removed and the new key/value pair is added without an exception being thrown.
The following example defines a MainClass class and a MainInfo class, which provides information about the MainClass instance. The example then calls the Add method to add a MainClass object and its attached MainInfo object to a ConditionalWeakTable<TKey, TValue> table. The example also illustrates calls to the GetOrCreateValue and GetValue methods to add key/value pairs to the table, and to the TryGetValue method to retrieve the value of an existing key.
- SecurityCriticalAttribute
requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.