Add Method
Collapse the table of content
Expand the table of content

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.

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.

ExceptionCondition
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.

Windows Phone OS

Supported in: 8.1, 8.0

  • SecurityCriticalAttribute 

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

Show:
© 2017 Microsoft