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.
Assembly: mscorlib (in mscorlib.dll)
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.
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