Class DictionaryExtensions
- Namespace
- NanoByte.Common.Collections
- Assembly
- NanoByte.Common.dll
Provides extension methods for System.Collections.Generic.Dictionary<TKey, TValue>s.
public static class DictionaryExtensions
- Inheritance
-
objectDictionaryExtensions
Methods
AddRange<TSourceKey, TSourceValue, TTargetKey, TTargetValue>(IDictionary<TTargetKey, TTargetValue>, IEnumerable<KeyValuePair<TSourceKey, TSourceValue>>)
Adds multiple pairs to the dictionary in one go.
public static void AddRange<TSourceKey, TSourceValue, TTargetKey, TTargetValue>(this IDictionary<TTargetKey, TTargetValue> target, IEnumerable<KeyValuePair<TSourceKey, TSourceValue>> source) where TSourceKey : TTargetKey where TSourceValue : TTargetValue where TTargetKey : notnull
Parameters
targetIDictionary<TTargetKey, TTargetValue>sourceIEnumerable<KeyValuePair<TSourceKey, TSourceValue>>
Type Parameters
TSourceKeyTSourceValueTTargetKeyTTargetValue
Deconstruct<TKey, TValue>(KeyValuePair<TKey, TValue>, out TKey, out TValue)
Deconstructs a System.Collections.Generic.KeyValuePair<TKey, TValue> like a tuple.
[Pure]
public static void Deconstruct<TKey, TValue>(this KeyValuePair<TKey, TValue> pair, out TKey key, out TValue value)
Parameters
pairKeyValuePair<TKey, TValue>keyTKeyvalueTValue
Type Parameters
TKeyTValue
Examples
foreach (var (key, value) in dictionary) {/.../}
GetOrAddAsync<TKey, TValue>(IDictionary<TKey, TValue>, TKey, Func<Task<TValue>>)
Returns an existing element with a specific key from a dictionary or creates and adds a new element using a callback if it is missing.
public static Task<TValue> GetOrAddAsync<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<Task<TValue>> valueFactory) where TKey : notnull
Parameters
dictionaryIDictionary<TKey, TValue>The dictionary to get an element from or to add an element to.
keyTKeyThe key to look for in the
dictionary.valueFactoryFunc<Task<TValue>>A callback that provides a task that provides the value to add to the
dictionaryif thekeyis not found.
Returns
- Task<TValue>
The existing element or the newly created element.
Type Parameters
TKeyTValue
Remarks
Superfluous calls to valueFactory may occur in case of read races. System.IDisposable.Dispose() is called on superfluously created objects if implemented.
GetOrAdd<TKey, TValue>(IDictionary<TKey, TValue>, TKey)
Returns an existing element with a specific key from a dictionary or creates and adds a new element using the default constructor if it is missing.
public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key) where TKey : notnull where TValue : new()
Parameters
dictionaryIDictionary<TKey, TValue>The dictionary to get an element from or to add an element to.
keyTKeyThe key to look for in the
dictionary.
Returns
- TValue
The existing element or the newly created element.
Type Parameters
TKeyTValue
GetOrAdd<TKey, TValue>(IDictionary<TKey, TValue>, TKey, Func<TValue>)
Returns an existing element with a specific key from a dictionary or creates and adds a new element using a callback if it is missing.
public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TValue> valueFactory) where TKey : notnull
Parameters
dictionaryIDictionary<TKey, TValue>The dictionary to get an element from or to add an element to.
keyTKeyThe key to look for in the
dictionary.valueFactoryFunc<TValue>A callback that provides the value to add to the
dictionaryif thekeyis not found.
Returns
- TValue
The existing element or the newly created element.
Type Parameters
TKeyTValue
Remarks
No superfluous calls to valueFactory occur. Not thread-safe!
GetUnsequencedHashCode<TKey, TValue>(IDictionary<TKey, TValue>, IEqualityComparer<TValue>?)
Generates a hash code for the contents of the dictionary.
[Pure]
public static int GetUnsequencedHashCode<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, IEqualityComparer<TValue>? valueComparer = null) where TKey : notnull
Parameters
dictionaryIDictionary<TKey, TValue>The dictionary to generate the hash for.
valueComparerIEqualityComparer<TValue>Controls how to compare values; leave
nullfor default comparer.
Returns
- int
Type Parameters
TKeyTValue
- See Also
GetValueOrDefault<TKey, TValue>(IReadOnlyDictionary<TKey, TValue>, TKey)
Returns an existing element with a specific key from a dictionary or the value type's default value if it is missing.
[Pure]
public static TValue? GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key)
Parameters
dictionaryIReadOnlyDictionary<TKey, TValue>The dictionary to get an element from.
keyTKeyThe key to look for in the
dictionary.
Returns
- TValue
The existing element or the default value of
TValue.
Type Parameters
TKeyTValue
GetValueOrDefault<TKey, TValue>(IReadOnlyDictionary<TKey, TValue>, TKey, TValue)
Returns an existing element with a specific key from a dictionary or a default value if it is missing.
[Pure]
public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
Parameters
dictionaryIReadOnlyDictionary<TKey, TValue>The dictionary to get an element from.
keyTKeyThe key to look for in the
dictionary.defaultValueTValueThe default value to return if the element is missing.
Returns
- TValue
The existing element or
defaultValue.
Type Parameters
TKeyTValue
ToMultiDictionary<TSource, TKey, TValue>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TSource, TValue>)
Builds a MultiDictionary<TKey, TValue> from an enumerable.
[Pure]
public static MultiDictionary<TKey, TValue> ToMultiDictionary<TSource, TKey, TValue>(this IEnumerable<TSource> elements, Func<TSource, TKey> keySelector, Func<TSource, TValue> valueSelector) where TKey : notnull
Parameters
elementsIEnumerable<TSource>The elements to build the dictionary from.
keySelectorFunc<TSource, TKey>Selects the dictionary key from an input element.
valueSelectorFunc<TSource, TValue>Selects a dictionary value from an input element.
Returns
- MultiDictionary<TKey, TValue>
Type Parameters
TSourceTKeyTValue
TryAdd<TKey, TValue>(IDictionary<TKey, TValue>, TKey, TValue)
Attempts to add the specified key and value to the dictionary.
public static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) where TKey : notnull
Parameters
dictionaryIDictionary<TKey, TValue>keyTKeyvalueTValue
Returns
- bool
trueif the key/value pair was added to the dictionary successfully;falsefalse if the key already existed in the dictionary.
Type Parameters
TKeyTValue
UnsequencedEquals<TKey, TValue>(IDictionary<TKey, TValue>, IDictionary<TKey, TValue>, IEqualityComparer<TValue>?)
Determines whether two dictionaries contain the same key-value pairs.
[Pure]
public static bool UnsequencedEquals<TKey, TValue>(this IDictionary<TKey, TValue> first, IDictionary<TKey, TValue> second, IEqualityComparer<TValue>? valueComparer = null) where TKey : notnull
Parameters
firstIDictionary<TKey, TValue>The first of the two dictionaries to compare.
secondIDictionary<TKey, TValue>The first of the two dictionaries to compare.
valueComparerIEqualityComparer<TValue>Controls how to compare values; leave
nullfor default comparer.
Returns
- bool
Type Parameters
TKeyTValue