ConditionalWeakTable<TKey, TValue>::Add Method (TKey, TValue)

 

Adds a key to the table.

Namespace:   System.Runtime.CompilerServices
Assembly:  mscorlib (in mscorlib.dll)

public:
void Add(
	TKey key,
	TValue value
)

Parameters

key
Type: TKey

The key to add. key represents the object to which the property is attached.

value
Type: TValue

The key's property value.

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.

No code example is currently available or this language may not be supported.

SecurityCriticalAttribute

requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

Universal Windows Platform
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
Return to top
Show: