ConditionalWeakTable<TKey, TValue>::Add Method (TKey, TValue)
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.
requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 4.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1