Table of Contents

Class DictionaryExtensions

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

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

public static class DictionaryExtensions
Inheritance
Object
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 System.Collections.Generic.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

System.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 System.Collections.Generic.IDictionary<TKey, TValue> to extend.

destination IDictionary<TKey, TValue>

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

Returns

IDictionary<TKey, TValue>

An System.Collections.Generic.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 System.Collections.Generic.IDictionary<TKey, TValue> to extend.

destination IDictionary<TKey, TValue>

The System.Collections.Generic.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 System.Collections.Generic.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 System.Collections.Generic.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

System.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 System.Collections.Generic.KeyValuePair`2 sequence.

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

Parameters

dictionary IDictionary<TKey, TValue>

The System.Collections.Generic.IDictionary<TKey, TValue> to extend.

Returns

IEnumerable<KeyValuePair<TKey, TValue>>

A System.Collections.Generic.KeyValuePair`2 equivalent sequence of dictionary.

Type Parameters

TKey

The System.Type of the key in the resulting System.Collections.Generic.KeyValuePair`2.

TValue

The System.Type of the value in the resulting System.Collections.Generic.KeyValuePair`2.

Exceptions

System.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 System.Collections.Generic.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

System.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 System.Collections.Generic.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

System.ArgumentNullException

dictionary cannot be null.