Table of Contents

Class DictionaryExtensions

Namespace
Cuemon.Extensions.Collections.Generic
Assembly
Cuemon.Extensions.Collections.Generic.dll

Extension methods for the IDictionary<TKey,TValue> interface.

public static class DictionaryExtensions
Inheritance
DictionaryExtensions

Methods

AddOrUpdate<TKey, TValue>(IDictionary<TKey, TValue>, TKey, TValue)

Attempts to add or update an existing element with the provided key to the dictionary with the specified value.

public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)

Parameters

dictionary IDictionary<TKey, TValue>

The IDictionary<TKey,TValue> to extend.

key TKey

The key of the element to add or update.

value TValue

The value of the element to add or update.

Type Parameters

TKey
TValue

Exceptions

ArgumentNullException

dictionary cannot be null -or- key cannot be null.

CopyTo<TKey, TValue>(IDictionary<TKey, TValue>, IDictionary<TKey, TValue>)

Copies all elements from source to destination.

public static IDictionary<TKey, TValue> CopyTo<TKey, TValue>(this IDictionary<TKey, TValue> source, IDictionary<TKey, TValue> destination)

Parameters

source IDictionary<TKey, TValue>

The IDictionary<TKey,TValue> to extend.

destination IDictionary<TKey, TValue>

The IDictionary<TKey,TValue> to which the elements of the source will be copied.

Returns

IDictionary<TKey, TValue>

An IDictionary<TKey,TValue> that is the result of the populated destination.

Type Parameters

TKey

The type of the keys in the dictionary.

TValue

The type of the values in the dictionary.

CopyTo<TKey, TValue>(IDictionary<TKey, TValue>, IDictionary<TKey, TValue>, Action<IDictionary<TKey, TValue>, IDictionary<TKey, TValue>>)

Copies elements from source to destination using the copier delegate.

public static IDictionary<TKey, TValue> CopyTo<TKey, TValue>(this IDictionary<TKey, TValue> source, IDictionary<TKey, TValue> destination, Action<IDictionary<TKey, TValue>, IDictionary<TKey, TValue>> copier)

Parameters

source IDictionary<TKey, TValue>

The IDictionary<TKey,TValue> to extend.

destination IDictionary<TKey, TValue>

The IDictionary<TKey,TValue> to which the elements of the source will be copied.

copier Action<IDictionary<TKey, TValue>, IDictionary<TKey, TValue>>

The delegate that will populate a copy of source to the specified destination.

Returns

IDictionary<TKey, TValue>

An IDictionary<TKey,TValue> that is the result of the populated destination.

Type Parameters

TKey

The type of the keys in the dictionary.

TValue

The type of the values in the dictionary.

GetValueOrDefault<TKey, TValue>(IDictionary<TKey, TValue>, TKey, Func<TValue>)

Gets the value associated with the specified key or a default value through defaultProvider when the key does not exists in the dictionary.

public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TValue> defaultProvider)

Parameters

dictionary IDictionary<TKey, TValue>

The IDictionary<TKey,TValue> to extend.

key TKey

The key of the value to get.

defaultProvider Func<TValue>

The function delegate that will provide a default value when the key does not exists in the dictionary.

Returns

TValue

Either the value associated with the specified key or a default value through defaultProvider when the key does not exists.

Type Parameters

TKey

The type of the keys in the dictionary.

TValue

The type of the values in the dictionary.

Exceptions

ArgumentNullException

dictionary cannot be null -or- key cannot be null -or- defaultProvider cannot be null.

ToEnumerable<TKey, TValue>(IDictionary<TKey, TValue>)

Returns the specified dictionary typed as KeyValuePair<TKey,TValue> sequence.

public static IEnumerable<KeyValuePair<TKey, TValue>> ToEnumerable<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)

Parameters

dictionary IDictionary<TKey, TValue>

The IDictionary<TKey,TValue> to extend.

Returns

IEnumerable<KeyValuePair<TKey, TValue>>

A KeyValuePair<TKey,TValue> equivalent sequence of dictionary.

Type Parameters

TKey

The Type of the key in the resulting KeyValuePair<TKey,TValue>.

TValue

The Type of the value in the resulting KeyValuePair<TKey,TValue>.

Exceptions

ArgumentNullException

dictionary is null.

TryAdd<TKey, TValue>(IDictionary<TKey, TValue>, TKey, TValue, Func<IDictionary<TKey, TValue>, Boolean>)

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, Func<IDictionary<TKey, TValue>, bool> condition)

Parameters

dictionary IDictionary<TKey, TValue>

The IDictionary<TKey,TValue> to extend.

key TKey

The key of the element to add.

value TValue

The value of the element to add.

condition Func<IDictionary<TKey, TValue>, Boolean>

The function delegate that specifies the condition for adding the element.

Returns

Boolean

true if the key/value pair was added to the dictionary successfully; otherwise, false.

Type Parameters

TKey
TValue

Exceptions

ArgumentNullException

dictionary cannot be null -or- key cannot be null -or- condition cannot be null.

TryGetValueOrFallback<TKey, TValue>(IDictionary<TKey, TValue>, TKey, Func<IEnumerable<TKey>, TKey>, out TValue)

Gets the value associated with the specified key.

public static bool TryGetValueOrFallback<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<IEnumerable<TKey>, TKey> fallbackKeySelector, out TValue value)

Parameters

dictionary IDictionary<TKey, TValue>

The IDictionary<TKey,TValue> to extend.

key TKey

The key of the value to get.

fallbackKeySelector Func<IEnumerable<TKey>, TKey>

The function delegate that will resolve an alternate key from the specified key.

value TValue

When this method returns, contains the value associated with the specified key or the alternate key resolved from fallbackKeySelector, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.

Returns

Boolean

true if the dictionary contains an element with the specified key or the alternate key resolved from fallbackKeySelector, false otherwise.

Type Parameters

TKey

The type of the keys in the dictionary.

TValue

The type of the values in the dictionary.

Exceptions

ArgumentNullException

dictionary cannot be null.