Class DictionaryExtensions
- Namespace
- LfrlAnvil.Extensions
- Assembly
- LfrlAnvil.Core.dll
Contains IDictionary<TKey, TValue> extension methods.
public static class DictionaryExtensions
- Inheritance
-
DictionaryExtensions
- Inherited Members
Methods
AddOrUpdate<TKey, TValue>(IDictionary<TKey, TValue>, TKey, TValue)
Adds a new entry or updates an existing one if key already exists.
public static AddOrUpdateResult AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
Parameters
dictionaryIDictionary<TKey, TValue>Source dictionary.
keyTKeyEntry's key.
valueTValueEntry's value.
Returns
- AddOrUpdateResult
Added when new entry has been added (provided
keydid not exist), otherwise Updated.
Type Parameters
TKeyDictionary key type.
TValueDictionary value type.
GetOrAddDefault<TKey, TValue>(IDictionary<TKey, TValue?>, TKey)
Reads a value associated with the specified key from the dictionary, if it exists.
Otherwise adds a new entry to the dictionary with default value and returns that value.
public static TValue? GetOrAddDefault<TKey, TValue>(this IDictionary<TKey, TValue?> dictionary, TKey key) where TKey : notnull
Parameters
dictionaryIDictionary<TKey, TValue>Source dictionary.
keyTKeyEntry's key.
Returns
- TValue
Value associated with the provided
key, if it exists, otherwise default value
Type Parameters
TKeyDictionary key type.
TValueDictionary value type.
GetOrAdd<TKey, TValue>(IDictionary<TKey, TValue>, TKey, Func<TValue>)
Reads a value associated with the specified key from the dictionary, if it exists.
Otherwise adds a new entry to the dictionary with the value returned
by the defaultValueProvider invocation and returns that value.
public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TValue> defaultValueProvider) where TKey : notnull
Parameters
dictionaryIDictionary<TKey, TValue>Source dictionary.
keyTKeyEntry's key.
defaultValueProviderFunc<TValue>Entry's value provider. Gets invoked only when entry with the provided
keydoes not exist in thedictionary.
Returns
- TValue
Value associated with the provided
key, if it exists, otherwise value returned bydefaultValueProvider.
Type Parameters
TKeyDictionary key type.
TValueDictionary value type.
GetOrAdd<TKey, TValue>(IDictionary<TKey, TValue>, TKey, Lazy<TValue>)
Reads a value associated with the specified key from the dictionary, if it exists.
Otherwise adds a new entry to the dictionary with the value returned
by the defaultValueProvider invocation and returns that value.
public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Lazy<TValue> defaultValueProvider) where TKey : notnull
Parameters
dictionaryIDictionary<TKey, TValue>Source dictionary.
keyTKeyEntry's key.
defaultValueProviderLazy<TValue>Entry's value provider. Gets invoked only when entry with the provided
keydoes not exist in thedictionary.
Returns
- TValue
Value associated with the provided
key, if it exists, otherwise value returned bydefaultValueProvider.
Type Parameters
TKeyDictionary key type.
TValueDictionary value type.
TryUpdate<TKey, TValue>(IDictionary<TKey, TValue>, TKey, TValue)
Attempts to update an existing entry in the dictionary with the specified key.
public static bool TryUpdate<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
Parameters
dictionaryIDictionary<TKey, TValue>Source dictionary.
keyTKeyEntry's key.
valueTValueEntry's value.
Returns
- bool
true when entry with the specified
keyexists, otherwise false.
Type Parameters
TKeyDictionary key type.
TValueDictionary value type.