Edit

Share via


ConditionalWeakTable<TKey,TValue>.GetOrAdd Method

Definition

Overloads

GetOrAdd(TKey, Func<TKey,TValue>)

Searches for a specified key in the table and returns the corresponding value. If the key does not exist in the table, the method invokes the supplied factory to create a value that is bound to the specified key.

GetOrAdd(TKey, TValue)

Searches for a specified key in the table and returns the corresponding value. If the key does not exist in the table, the method adds the given value and binds it to the specified key.

GetOrAdd<TArg>(TKey, Func<TKey,TArg,TValue>, TArg)

Searches for a specified key in the table and returns the corresponding value. If the key does not exist in the table, the method invokes the supplied factory to create a value that is bound to the specified key.

GetOrAdd(TKey, Func<TKey,TValue>)

Source:
ConditionalWeakTable.cs

Searches for a specified key in the table and returns the corresponding value. If the key does not exist in the table, the method invokes the supplied factory to create a value that is bound to the specified key.

public:
 TValue GetOrAdd(TKey key, Func<TKey, TValue> ^ valueFactory);
public TValue GetOrAdd(TKey key, Func<TKey,TValue> valueFactory);
member this.GetOrAdd : 'Key * Func<'Key, 'Value (requires 'Key : null and 'Value : null)> -> 'Value
Public Function GetOrAdd (key As TKey, valueFactory As Func(Of TKey, TValue)) As TValue

Parameters

key
TKey

The key of the value to find. It cannot be null.

valueFactory
Func<TKey,TValue>

The callback that creates a value for key, if one does not exist already. It cannot be null.

Returns

TValue

The value bound to TKey in the current ConditionalWeakTable<TKey,TValue> instance, after the method completes.

Exceptions

key or valueFactory is null.

Remarks

If multiple threads try to initialize the same key, the table may invoke valueFactory multiple times with the same key. Exactly one of these calls will succeed and the returned value of that call will be the one added to the table and returned by all the racing GetOrAdd(TKey, Func<TKey,TValue>) calls. This rule permits the table to invoke valueFactory outside the internal table lock, to prevent deadlocks.

Applies to

GetOrAdd(TKey, TValue)

Source:
ConditionalWeakTable.cs

Searches for a specified key in the table and returns the corresponding value. If the key does not exist in the table, the method adds the given value and binds it to the specified key.

public:
 TValue GetOrAdd(TKey key, TValue value);
public TValue GetOrAdd(TKey key, TValue value);
member this.GetOrAdd : 'Key * 'Value -> 'Value
Public Function GetOrAdd (key As TKey, value As TValue) As TValue

Parameters

key
TKey

The key of the value to find. It cannot be null.

value
TValue

The value to add and bind to TKey, if one does not exist already.

Returns

TValue

The value bound to TKey in the current ConditionalWeakTable<TKey,TValue> instance, after the method completes.

Exceptions

key is null.

Applies to

GetOrAdd<TArg>(TKey, Func<TKey,TArg,TValue>, TArg)

Source:
ConditionalWeakTable.cs

Searches for a specified key in the table and returns the corresponding value. If the key does not exist in the table, the method invokes the supplied factory to create a value that is bound to the specified key.

public:
generic <typename TArg>
 TValue GetOrAdd(TKey key, Func<TKey, TArg, TValue> ^ valueFactory, TArg factoryArgument);
public TValue GetOrAdd<TArg>(TKey key, Func<TKey,TArg,TValue> valueFactory, TArg factoryArgument) where TArg : allows ref struct;
member this.GetOrAdd : 'Key * Func<'Key, 'Arg, 'Value (requires 'Key : null and 'Value : null)> * 'Arg -> 'Value
Public Function GetOrAdd(Of TArg) (key As TKey, valueFactory As Func(Of TKey, TArg, TValue), factoryArgument As TArg) As TValue

Type Parameters

TArg

The type of the additional argument to use with the value factory.

Parameters

key
TKey

The key of the value to find. It cannot be null.

valueFactory
Func<TKey,TArg,TValue>

The callback that creates a value for key, if one does not exist already. It cannot be null.

factoryArgument
TArg

The additional argument to supply to valueFactory upon invocation.

Returns

TValue

The value bound to TKey in the current ConditionalWeakTable<TKey,TValue> instance, after the method completes.

Exceptions

key or valueFactory is null.

Remarks

If multiple threads try to initialize the same key, the table may invoke valueFactory multiple times with the same key. Exactly one of these calls will succeed and the returned value of that call will be the one added to the table and returned by all the racing GetOrAdd<TArg>(TKey, Func<TKey,TArg,TValue>, TArg) calls. This rule permits the table to invoke valueFactory outside the internal table lock, to prevent deadlocks.

Applies to