Table of Contents

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
object
DictionaryExtensions

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

target IDictionary<TTargetKey, TTargetValue>
source IEnumerable<KeyValuePair<TSourceKey, TSourceValue>>

Type Parameters

TSourceKey
TSourceValue
TTargetKey
TTargetValue

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

pair KeyValuePair<TKey, TValue>
key TKey
value TValue

Type Parameters

TKey
TValue

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

dictionary IDictionary<TKey, TValue>

The dictionary to get an element from or to add an element to.

key TKey

The key to look for in the dictionary.

valueFactory Func<Task<TValue>>

A callback that provides a task that provides the value to add to the dictionary if the key is not found.

Returns

Task<TValue>

The existing element or the newly created element.

Type Parameters

TKey
TValue

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

dictionary IDictionary<TKey, TValue>

The dictionary to get an element from or to add an element to.

key TKey

The key to look for in the dictionary.

Returns

TValue

The existing element or the newly created element.

Type Parameters

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.

public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TValue> valueFactory) where TKey : notnull

Parameters

dictionary IDictionary<TKey, TValue>

The dictionary to get an element from or to add an element to.

key TKey

The key to look for in the dictionary.

valueFactory Func<TValue>

A callback that provides the value to add to the dictionary if the key is not found.

Returns

TValue

The existing element or the newly created element.

Type Parameters

TKey
TValue

Remarks

No superfluous calls to valueFactory occur. Not thread-safe!

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.

[Pure]
public static TValue? GetOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key) where TKey : notnull

Parameters

dictionary IDictionary<TKey, TValue>

The dictionary to get an element from.

key TKey

The key to look for in the dictionary.

Returns

TValue

The existing element or the default value of TValue.

Type Parameters

TKey
TValue

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

dictionary IDictionary<TKey, TValue>

The dictionary to generate the hash for.

valueComparer IEqualityComparer<TValue>

Controls how to compare values; leave null for default comparer.

Returns

int

Type Parameters

TKey
TValue
See Also

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

elements IEnumerable<TSource>

The elements to build the dictionary from.

keySelector Func<TSource, TKey>

Selects the dictionary key from an input element.

valueSelector Func<TSource, TValue>

Selects a dictionary value from an input element.

Returns

MultiDictionary<TKey, TValue>

Type Parameters

TSource
TKey
TValue

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

dictionary IDictionary<TKey, TValue>
key TKey
value TValue

Returns

bool

true if the key/value pair was added to the dictionary successfully; false false if the key already existed in the dictionary.

Type Parameters

TKey
TValue

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

first IDictionary<TKey, TValue>

The first of the two dictionaries to compare.

second IDictionary<TKey, TValue>

The first of the two dictionaries to compare.

valueComparer IEqualityComparer<TValue>

Controls how to compare values; leave null for default comparer.

Returns

bool

Type Parameters

TKey
TValue