Class DictionaryUtil
- Namespace
- Hi.Common.Collections
- Assembly
- HiGeom.dll
Utility class providing extension methods for dictionary operations.
public static class DictionaryUtil
- Inheritance
-
DictionaryUtil
- Inherited Members
Methods
GetOrCreate<TKey, TValue>(IDictionary<TKey, TValue>, TKey)
Gets the value for key, or creates a new TValue
via its parameterless constructor, stores it in the dictionary, and returns it.
public static TValue GetOrCreate<TKey, TValue>(this IDictionary<TKey, TValue> src, TKey key) where TValue : new()
Parameters
srcIDictionary<TKey, TValue>keyTKey
Returns
- TValue
Type Parameters
TKeyTValue
GetOrCreate<TKey, TValue>(IDictionary<TKey, TValue>, TKey, Func<TValue>)
Gets the value for key, or invokes factory
to create, store, and return a new value if the key is absent.
public static TValue GetOrCreate<TKey, TValue>(this IDictionary<TKey, TValue> src, TKey key, Func<TValue> factory)
Parameters
srcIDictionary<TKey, TValue>keyTKeyfactoryFunc<TValue>
Returns
- TValue
Type Parameters
TKeyTValue
GetOrCreate<TKey, TValue>(IDictionary<TKey, TValue>, TKey, TValue)
Gets the value for key, or stores and returns
defaultValue if the key is absent.
public static TValue GetOrCreate<TKey, TValue>(this IDictionary<TKey, TValue> src, TKey key, TValue defaultValue)
Parameters
srcIDictionary<TKey, TValue>keyTKeydefaultValueTValue
Returns
- TValue
Type Parameters
TKeyTValue
Retrieve<K, V>(Dictionary<K, V>, K, out V, bool)
Retrieves a value from a dictionary by key, with an option to remove it from the source.
public static bool Retrieve<K, V>(this Dictionary<K, V> src, K key, out V v, bool removeFromSource)
Parameters
srcDictionary<K, V>The source dictionary.
keyKThe key to look for.
vVWhen this method returns, contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter.
removeFromSourceboolIf true, removes the key-value pair from the dictionary if found.
Returns
- bool
true if the key was found; otherwise, false.
Type Parameters
KThe type of the keys in the dictionary.
VThe type of the values in the dictionary.
TryGetValueByKeys<TKey, TValue>(IDictionary<TKey, TValue>, IEnumerable<TKey>, out TValue)
Tries to get a value from a dictionary by checking multiple keys in sequence.
public static bool TryGetValueByKeys<TKey, TValue>(this IDictionary<TKey, TValue> src, IEnumerable<TKey> keys, out TValue v)
Parameters
srcIDictionary<TKey, TValue>The source dictionary.
keysIEnumerable<TKey>The collection of keys to check.
vTValueWhen this method returns, contains the value associated with the first matching key, if a key is found; otherwise, the default value for the type of the value parameter.
Returns
- bool
true if any of the keys was found; otherwise, false.
Type Parameters
TKeyThe type of the keys in the dictionary.
TValueThe type of the values in the dictionary.