ConditionalWeakTable<TKey, TValue>::CreateValueCallback Delegate

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.

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

Silverlight

Supported in: 5, 4

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show: