NanoByte.Common  2.6.1
NanoByte.Common.Collections.DictionaryExtensions Class Reference

Provides extension methods for Dictionary<TKey,TValue>s. More...

Static Public Member Functions

static void AddRange< TSourceKey, TSourceValue, TTargetKey, TTargetValue > (this IDictionary< TTargetKey, TTargetValue > target, IEnumerable< KeyValuePair< TSourceKey, TSourceValue >> source)
 Adds multiple pairs to the dictionary in one go. More...
 
static TValue GetOrDefault< TKey, TValue > (this IDictionary< TKey, TValue > dictionary, TKey key)
 Returns an existing element with a specific key from a dictionary or the value type's default value if it is missing. More...
 
static TValue GetOrAdd< TKey, TValue > (this IDictionary< TKey, TValue > dictionary, TKey key, Func< TValue > valueFactory)
 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. More...
 
static TValue GetOrAdd< TKey, TValue > (this IDictionary< TKey, TValue > dictionary, TKey key)
 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. More...
 
static async Task< TValue > GetOrAddAsync< TKey, TValue > (this IDictionary< TKey, TValue > dictionary, TKey key, Func< Task< TValue >> valueFactory)
 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. More...
 
static MultiDictionary< TKey, TValue > ToMultiDictionary< TSource, TKey, TValue > (this IEnumerable< TSource > elements, Func< TSource, TKey > keySelector, Func< TSource, TValue > valueSelector)
 Builds a MultiDictionary<TKey,TValue> from an enumerable. More...
 
static bool UnsequencedEquals< TKey, TValue > (this IDictionary< TKey, TValue > first, IDictionary< TKey, TValue > second, IEqualityComparer< TValue >? valueComparer=null)
 Determines whether two dictionaries contain the same key-value pairs. More...
 
static int GetUnsequencedHashCode< TKey, TValue > (this IDictionary< TKey, TValue > dictionary, IEqualityComparer< TValue >? valueComparer=null)
 Generates a hash code for the contents of the dictionary. More...
 
static void Deconstruct< TKey, TValue > (this KeyValuePair< TKey, TValue > pair, out TKey key, out TValue value)
 Deconstructs a KeyValuePair<TKey,TValue> like a tuple. More...
 

Detailed Description

Provides extension methods for Dictionary<TKey,TValue>s.

Member Function Documentation

◆ AddRange< TSourceKey, TSourceValue, TTargetKey, TTargetValue >()

static void NanoByte.Common.Collections.DictionaryExtensions.AddRange< TSourceKey, TSourceValue, TTargetKey, TTargetValue > ( this IDictionary< TTargetKey, TTargetValue >  target,
IEnumerable< KeyValuePair< TSourceKey, TSourceValue >>  source 
)
inlinestatic

Adds multiple pairs to the dictionary in one go.

Type Constraints
TSourceKey :TTargetKey 
TSourceValue :TTargetValue 

◆ Deconstruct< TKey, TValue >()

static void NanoByte.Common.Collections.DictionaryExtensions.Deconstruct< TKey, TValue > ( this KeyValuePair< TKey, TValue >  pair,
out TKey  key,
out TValue  value 
)
inlinestatic

Deconstructs a KeyValuePair<TKey,TValue> like a tuple.

foreach (var (key, value) in dictionary) {/*...*‍/}

◆ GetOrAdd< TKey, TValue >() [1/2]

static TValue NanoByte.Common.Collections.DictionaryExtensions.GetOrAdd< TKey, TValue > ( this IDictionary< TKey, TValue >  dictionary,
TKey  key 
)
inlinestatic

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.

Parameters
dictionaryThe dictionary to get an element from or to add an element to.
keyThe key to look for in the dictionary .
Returns
The existing element or the newly created element.
Type Constraints
TValue :new() 

◆ GetOrAdd< TKey, TValue >() [2/2]

static TValue NanoByte.Common.Collections.DictionaryExtensions.GetOrAdd< TKey, TValue > ( this IDictionary< TKey, TValue >  dictionary,
TKey  key,
Func< TValue >  valueFactory 
)
inlinestatic

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.

Parameters
dictionaryThe dictionary to get an element from or to add an element to.
keyThe key to look for in the dictionary .
valueFactoryA callback that provides the value to add to the dictionary if the key is not found.
Returns
The existing element or the newly created element.

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

◆ GetOrAddAsync< TKey, TValue >()

static async Task<TValue> NanoByte.Common.Collections.DictionaryExtensions.GetOrAddAsync< TKey, TValue > ( this IDictionary< TKey, TValue >  dictionary,
TKey  key,
Func< Task< TValue >>  valueFactory 
)
inlinestatic

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.

Parameters
dictionaryThe dictionary to get an element from or to add an element to.
keyThe key to look for in the dictionary .
valueFactoryA callback that provides a task that provides the value to add to the dictionary if the key is not found.
Returns
The existing element or the newly created element.

Superfluous calls to valueFactory may occur in case of read races. IDisposable.Dispose is called on superfluously created objects if implemented.

◆ GetOrDefault< TKey, TValue >()

static TValue NanoByte.Common.Collections.DictionaryExtensions.GetOrDefault< TKey, TValue > ( this IDictionary< TKey, TValue >  dictionary,
TKey  key 
)
inlinestatic

Returns an existing element with a specific key from a dictionary or the value type's default value if it is missing.

Parameters
dictionaryThe dictionary to get an element from.
keyThe key to look for in the dictionary .
Returns
The existing element or the default value of TValue .

◆ GetUnsequencedHashCode< TKey, TValue >()

static int NanoByte.Common.Collections.DictionaryExtensions.GetUnsequencedHashCode< TKey, TValue > ( this IDictionary< TKey, TValue >  dictionary,
IEqualityComparer< TValue >?  valueComparer = null 
)
inlinestatic

Generates a hash code for the contents of the dictionary.

Parameters
dictionaryThe dictionary to generate the hash for.
valueComparerControls how to compare values; leave null for default comparer.
See also
UnsequencedEquals<TKey,TValue>

◆ ToMultiDictionary< TSource, TKey, TValue >()

static MultiDictionary<TKey, TValue> NanoByte.Common.Collections.DictionaryExtensions.ToMultiDictionary< TSource, TKey, TValue > ( this IEnumerable< TSource >  elements,
Func< TSource, TKey >  keySelector,
Func< TSource, TValue >  valueSelector 
)
inlinestatic

Builds a MultiDictionary<TKey,TValue> from an enumerable.

Parameters
elementsThe elements to build the dictionary from.
keySelectorSelects the dictionary key from an input element.
valueSelectorSelects a dictionary value from an input element.
Type Constraints
TKey :notnull 

◆ UnsequencedEquals< TKey, TValue >()

static bool NanoByte.Common.Collections.DictionaryExtensions.UnsequencedEquals< TKey, TValue > ( this IDictionary< TKey, TValue >  first,
IDictionary< TKey, TValue >  second,
IEqualityComparer< TValue >?  valueComparer = null 
)
inlinestatic

Determines whether two dictionaries contain the same key-value pairs.

Parameters
firstThe first of the two dictionaries to compare.
secondThe first of the two dictionaries to compare.
valueComparerControls how to compare values; leave null for default comparer.

The documentation for this class was generated from the following file: