Class DictionaryExtensions
- Namespace
- Olve.Utilities.CollectionExtensions
- Assembly
- Olve.Utilities.dll
Provides a set of static methods for querying and modifying objects that implement IDictionary<TKey, TValue>.
public static class DictionaryExtensions
- Inheritance
-
DictionaryExtensions
- Inherited Members
Remarks
These methods utilize GetValueRefOrAddDefault<TKey, TValue>(Dictionary<TKey, TValue>, TKey, out bool) and related APIs, which provide direct references to dictionary values. They should only be used in single-threaded contexts, as concurrent modifications can result in undefined behavior.
Methods
GetOrAdd<TKey, TValue>(Dictionary<TKey, TValue>, TKey, Func<TValue>)
Gets the value associated with the specified key or adds a new value if the key does not exist.
public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, Func<TValue> valueFactory) where TKey : notnull
Parameters
dictionaryDictionary<TKey, TValue>The dictionary to get or add the value from.
keyTKeyThe key of the value to get or add.
valueFactoryFunc<TValue>The function used to generate a new value for the key.
Returns
- TValue
The value associated with the specified key or the newly added value.
Type Parameters
TKeyThe type of the keys in the dictionary.
TValueThe type of the values in the dictionary.
Remarks
This method provides a direct reference to the dictionary's internal storage and should be used only in single-threaded scenarios.
GetOrAdd<TKey, TValue>(Dictionary<TKey, TValue>, TKey, TValue)
Gets the value associated with the specified key or adds a new value if the key does not exist.
public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value) where TKey : notnull
Parameters
dictionaryDictionary<TKey, TValue>The dictionary to get or add the value from.
keyTKeyThe key of the value to get or add.
valueTValueThe value to add if the key does not exist.
Returns
- TValue
The value associated with the specified key or the newly added value.
Type Parameters
TKeyThe type of the keys in the dictionary.
TValueThe type of the values in the dictionary.
Remarks
This method should be used in single-threaded contexts due to its direct manipulation of dictionary storage.
TryUpdate<TKey, TValue>(Dictionary<TKey, TValue>, TKey, Func<TValue, TValue>)
Updates the value associated with the specified key using a transformation function if the key exists.
public static bool TryUpdate<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, Func<TValue, TValue> update) where TKey : notnull
Parameters
dictionaryDictionary<TKey, TValue>The dictionary in which to update the value.
keyTKeyThe key of the value to update.
updateFunc<TValue, TValue>The function used to update the existing value.
Returns
Type Parameters
TKeyThe type of the keys in the dictionary.
TValueThe type of the values in the dictionary.
Remarks
This method should be used only in single-threaded contexts, as it modifies dictionary values directly.
TryUpdate<TKey, TValue>(Dictionary<TKey, TValue>, TKey, TValue)
Updates the value associated with the specified key if the key exists.
public static bool TryUpdate<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value) where TKey : notnull
Parameters
dictionaryDictionary<TKey, TValue>The dictionary in which to update the value.
keyTKeyThe key of the value to update.
valueTValueThe new value to set.
Returns
Type Parameters
TKeyThe type of the keys in the dictionary.
TValueThe type of the values in the dictionary.
Remarks
This method should be used in single-threaded scenarios as it provides a direct reference to the dictionary's internal storage.