DictionaryExtensions Class

Provides extension methods for Dictionary<TKey, TValue>.

public static class DictionaryExtensions

Methods

NameDescription
GetOrAdd<TKey, TValue>(Dictionary<TKey, TValue>, TKey, Func<TKey, TValue>)Gets the value associated with the specified key or adds it using the factory function if it doesn't exist.
GetOrAdd<TKey, TValue>(Dictionary<TKey, TValue>, TKey, TValue)Gets the value associated with the specified key or adds it if it doesn't exist.
TryUpdate<TKey, TValue>(Dictionary<TKey, TValue>, TKey, Func<TKey, TValue, TValue>)Attempts to update the value associated with the specified key using the factory function.
TryUpdate<TKey, TValue>(Dictionary<TKey, TValue>, TKey, TValue)Attempts to update the value associated with the specified key.

GetOrAdd overloads

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

DictionaryExtensions.cs

Gets the value associated with the specified key or adds it using the factory function if it doesn't exist.

public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, Func<TKey, TValue> valueFactory) where TKey : notnull
Type parameters
TKey
TValue
Parameters
dict Dictionary<TKey, TValue>
key TKey
valueFactory Func<TKey, TValue>
Returns

TValue

Remarks

The key is only added once valueFactory has returned, so a factory that throws leaves the dictionary unchanged.

GetOrAdd<TKey, TValue>(Dictionary<TKey, TValue>, TKey, TValue)

DictionaryExtensions.cs

Gets the value associated with the specified key or adds it if it doesn't exist.

public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue value) where TKey : notnull
Type parameters
TKey
TValue
Parameters
dict Dictionary<TKey, TValue>
key TKey
value TValue
Returns

TValue

TryUpdate overloads

TryUpdate<TKey, TValue>(Dictionary<TKey, TValue>, TKey, Func<TKey, TValue, TValue>)

DictionaryExtensions.cs

Attempts to update the value associated with the specified key using the factory function.

public static bool TryUpdate<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, Func<TKey, TValue, TValue> valueFactory) where TKey : notnull
Type parameters
TKey
TValue
Parameters
dict Dictionary<TKey, TValue>
key TKey
valueFactory Func<TKey, TValue, TValue>
Returns

bool

TryUpdate<TKey, TValue>(Dictionary<TKey, TValue>, TKey, TValue)

DictionaryExtensions.cs

Attempts to update the value associated with the specified key.

public static bool TryUpdate<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue value) where TKey : notnull
Type parameters
TKey
TValue
Parameters
dict Dictionary<TKey, TValue>
key TKey
value TValue
Returns

bool