Class DictionaryExtensions
Provides extension methods for Dictionary<TKey,TValue>s.
Namespace: NanoByte.Common.Collections
Assembly: NanoByte.Common.dll
Syntax
public static class DictionaryExtensions : Object
Methods
AddRange<TSourceKey, TSourceValue, TTargetKey, TTargetValue>(IDictionary<TTargetKey, TTargetValue>, IEnumerable<KeyValuePair<TSourceKey, TSourceValue>>)
Adds multiple pairs to the dictionary in one go.
Declaration
public static void AddRange<TSourceKey, TSourceValue, TTargetKey, TTargetValue>(this IDictionary<TTargetKey, TTargetValue> target, IEnumerable<KeyValuePair<TSourceKey, TSourceValue>> source)
where TSourceKey : TTargetKey where TSourceValue : TTargetValue
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IDictionary<TTargetKey, TTargetValue> | target | |
IEnumerable<KeyValuePair<TSourceKey, TSourceValue>> | source |
Type Parameters
Name | Description |
---|---|
TSourceKey | |
TSourceValue | |
TTargetKey | |
TTargetValue |
Deconstruct<TKey, TValue>(KeyValuePair<TKey, TValue>, out TKey, out TValue)
Deconstructs a KeyValuePair<TKey,TValue> like a tuple.
Declaration
public static void Deconstruct<TKey, TValue>(this KeyValuePair<TKey, TValue> pair, out TKey key, out TValue value)
Parameters
Type | Name | Description |
---|---|---|
KeyValuePair<TKey, TValue> | pair | |
TKey | key | |
TValue | value |
Type Parameters
Name | Description |
---|---|
TKey | |
TValue |
Examples
foreach (var (key, value) in dictionary) {/.../}
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.
Declaration
public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
where TValue : new()
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IDictionary<TKey, TValue> | dictionary | The dictionary to get an element from or to add an element to. |
TKey | key | The key to look for in the |
Returns
Type | Description |
---|---|
TValue | The existing element or the newly created element. |
Type Parameters
Name | Description |
---|---|
TKey | |
TValue |
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.
Declaration
public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TValue> valueFactory)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IDictionary<TKey, TValue> | dictionary | The dictionary to get an element from or to add an element to. |
TKey | key | The key to look for in the |
Func<TValue> | valueFactory | A callback that provides the value to add to the |
Returns
Type | Description |
---|---|
TValue | The existing element or the newly created element. |
Type Parameters
Name | Description |
---|---|
TKey | |
TValue |
Remarks
No superfluous calls to valueFactory
occur. Not thread-safe!
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.
Declaration
public static Task<TValue> GetOrAddAsync<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<Task<TValue>> valueFactory)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IDictionary<TKey, TValue> | dictionary | The dictionary to get an element from or to add an element to. |
TKey | key | The key to look for in the |
Func<System.Threading.Tasks.Task<TValue>> | valueFactory | A callback that provides a task that provides the value to add to the |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<TValue> | The existing element or the newly created element. |
Type Parameters
Name | Description |
---|---|
TKey | |
TValue |
Remarks
Superfluous calls to valueFactory
may occur in case of read races. Dispose() is called on superfluously created objects if implemented.
GetOrDefault<TKey, TValue>(IDictionary<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.
Declaration
public static TValue GetOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IDictionary<TKey, TValue> | dictionary | The dictionary to get an element from. |
TKey | key | The key to look for in the |
Returns
Type | Description |
---|---|
TValue | The existing element or the default value of |
Type Parameters
Name | Description |
---|---|
TKey | |
TValue |
GetUnsequencedHashCode<TKey, TValue>(IDictionary<TKey, TValue>, IEqualityComparer<TValue>)
Generates a hash code for the contents of the dictionary.
Declaration
public static int GetUnsequencedHashCode<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, IEqualityComparer<TValue> valueComparer = null)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IDictionary<TKey, TValue> | dictionary | The dictionary to generate the hash for. |
IEqualityComparer<TValue> | valueComparer | Controls how to compare values; leave |
Returns
Type | Description |
---|---|
Int32 |
Type Parameters
Name | Description |
---|---|
TKey | |
TValue |
See Also
ToMultiDictionary<TSource, TKey, TValue>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TSource, TValue>)
Builds a MultiDictionary<TKey, TValue> from an enumerable.
Declaration
public static MultiDictionary<TKey, TValue> ToMultiDictionary<TSource, TKey, TValue>(this IEnumerable<TSource> elements, Func<TSource, TKey> keySelector, Func<TSource, TValue> valueSelector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | elements | The elements to build the dictionary from. |
Func<TSource, TKey> | keySelector | Selects the dictionary key from an input element. |
Func<TSource, TValue> | valueSelector | Selects a dictionary value from an input element. |
Returns
Type | Description |
---|---|
MultiDictionary<TKey, TValue> |
Type Parameters
Name | Description |
---|---|
TSource | |
TKey | |
TValue |
TryAdd<TKey, TValue>(IDictionary<TKey, TValue>, TKey, TValue)
Attempts to add the specified key and value to the dictionary.
Declaration
public static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IDictionary<TKey, TValue> | dictionary | |
TKey | key | |
TValue | value |
Returns
Type | Description |
---|---|
Boolean |
|
Type Parameters
Name | Description |
---|---|
TKey | |
TValue |
UnsequencedEquals<TKey, TValue>(IDictionary<TKey, TValue>, IDictionary<TKey, TValue>, IEqualityComparer<TValue>)
Determines whether two dictionaries contain the same key-value pairs.
Declaration
public static bool UnsequencedEquals<TKey, TValue>(this IDictionary<TKey, TValue> first, IDictionary<TKey, TValue> second, IEqualityComparer<TValue> valueComparer = null)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IDictionary<TKey, TValue> | first | The first of the two dictionaries to compare. |
System.Collections.Generic.IDictionary<TKey, TValue> | second | The first of the two dictionaries to compare. |
IEqualityComparer<TValue> | valueComparer | Controls how to compare values; leave |
Returns
Type | Description |
---|---|
Boolean |
Type Parameters
Name | Description |
---|---|
TKey | |
TValue |