ConditionalWeakTable(TKey, TValue).CreateValueCallback Delegate
TOC
Collapse the table of content
Expand the table of content

ConditionalWeakTable<TKey, TValue>::CreateValueCallback Delegate

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Represents a method that creates a non-default value to add as part of a key/value pair to a ConditionalWeakTable<TKey, TValue> object.

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

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

Parameters

key
Type: TKey
The key that belongs to the value to create.

Return Value

Type: TValue
An instance of a reference type that represents the value to attach to the specified key.

The ConditionalWeakTable<TKey, TValue>::CreateValueCallback delegate encapsulates a callback method that is invoked by the ConditionalWeakTable<TKey, TValue>::GetValue method when the key passed to the method is not found in the ConditionalWeakTable<TKey, TValue> object. The GetValue method passes the callback method the key that represents a managed object to which a property value is to be dynamically attached. The method is responsible for returning the property value to its caller, which, in turn, adds the key/value pair to the table.

The ConditionalWeakTable<TKey, TValue>::CreateValueCallback method is used to return an instance of a reference type that can be attached to the specified key. It enables that instance to be initialized using non-default values. If the key is not found in the table, the ConditionalWeakTable<TKey, TValue>::GetOrCreateValue method adds a key/value pair in which the value is initialized using default values.

The following example defines a MainClass class and a MainInfo class. MainInfo provides information about the MainClass instance. It also defines a static (Shared in Visual Basic) CreateAttachedValue method that can be assigned to the ConditionalWeakTable<TKey, TValue>::CreateValueCallback delegate and passed to the GetValue method. The example calls the GetValue method to add a MainClass object and its corresponding MainInfo object to a ConditionalWeakTable<TKey, TValue> table. The example also illustrates calls to the Add and GetOrCreateValue methods to add key/value pairs to the table, and a call to the TryGetValue method to retrieve the value that belongs to an existing key.

Windows Phone OS

Supported in: 8.1, 8.0

Show:
© 2017 Microsoft