Table of Contents

Class IDictionaryExtensions

Namespace
Garyon.Extensions
Assembly
Garyon.dll

Provides useful extensions for the IDictionary<TKey, TValue> and IDictionary types.

public static class IDictionaryExtensions
Inheritance
IDictionaryExtensions
Inherited Members

Methods

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

Adds a collection of new entries to the dictionary. For each of the given entries, if its key already exists, its value is overwritten in the source dictionary.

public static bool AddOrSetRange<TKey, TValue>(this IDictionary<TKey, TValue> source, IDictionary<TKey, TValue> entries) where TKey : notnull

Parameters

source IDictionary<TKey, TValue>

The source dictionary.

entries IDictionary<TKey, TValue>

The entries to add or overwrite.

Returns

bool

true if at least one of the given entries already existed with a different value and was overwritten, otherwise false.

Type Parameters

TKey

The type of the keys stored in the dictionary.

TValue

The type of the values stored in the dictionary.

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

Adds a new entry to the dictionary. If the given key already exists, its value is overwritten in the source dictionary.

public static bool AddOrSet<TKey, TValue>(this IDictionary<TKey, TValue> source, TKey key, TValue value) where TKey : notnull

Parameters

source IDictionary<TKey, TValue>

The source dictionary.

key TKey

The key of the entry to add or overwrite.

value TValue

The value of the entry to set.

Returns

bool

true if the entry already existed with a different value and was overwritten, otherwise false.

Type Parameters

TKey

The type of the keys stored in the dictionary.

TValue

The type of the values stored in the dictionary.

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

Adds a range of entries to a source dictionary.

public static void AddRange<TKey, TValue>(this IDictionary<TKey, TValue> source, IDictionary<TKey, TValue> entries) where TKey : notnull

Parameters

source IDictionary<TKey, TValue>

The source dictionary.

entries IDictionary<TKey, TValue>

The entries to add to the source dictionary.

Type Parameters

TKey

The type of the keys stored in the dictionary.

TValue

The type of the values stored in the dictionary.

Add<TKey, TValue>(IDictionary<TKey, TValue>, KeyValuePair<TKey, TValue>)

Adds an entry to the provided dictionary, if the key does not exist.

public static void Add<TKey, TValue>(this IDictionary<TKey, TValue> source, KeyValuePair<TKey, TValue> kvp) where TKey : notnull

Parameters

source IDictionary<TKey, TValue>

The source dictionary.

kvp KeyValuePair<TKey, TValue>

The entry to add to the dictionary.

Type Parameters

TKey

The type of the keys stored in the dictionary.

TValue

The type of the values stored in the dictionary.

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

Gets a KeyValuePair<TKey, TValue> instance containing the specified key and its associated value.

public static KeyValuePair<TKey, TValue> GetKeyValuePair<TKey, TValue>(this IDictionary<TKey, TValue> source, TKey key) where TKey : notnull

Parameters

source IDictionary<TKey, TValue>

The source dictionary.

key TKey

The key of the entry whose KeyValuePair<TKey, TValue> to get.

Returns

KeyValuePair<TKey, TValue>

The resulting KeyValuePair<TKey, TValue> containing the entry that was requested.

Type Parameters

TKey

The type of the keys stored in the dictionary.

TValue

The type of the values stored in the dictionary.

IncrementOrAddKeyValue<TKey>(IDictionary<TKey, int>, TKey)

Increments the value of a key if it exists, otherwise creates a new key with the default value 1.

public static void IncrementOrAddKeyValue<TKey>(this IDictionary<TKey, int> d, TKey key) where TKey : notnull

Parameters

d IDictionary<TKey, int>

The dictionary whose value to increment for the specified key.

key TKey

The key in the dictionary whose value to increment.

Type Parameters

TKey

The type of the keys in the dictionary.

TransformKeys<TKey, TValue, TNewKey>(IReadOnlyDictionary<TKey, TValue>, Func<TKey, TNewKey>)

Transforms the keys of the source dictionary into new keys using the provided key selector function, and constructs a new dictionary with the transformed keys and the original values.

public static Dictionary<TNewKey, TValue> TransformKeys<TKey, TValue, TNewKey>(this IReadOnlyDictionary<TKey, TValue> source, Func<TKey, TNewKey> keySelector) where TKey : notnull where TNewKey : notnull

Parameters

source IReadOnlyDictionary<TKey, TValue>
keySelector Func<TKey, TNewKey>

Returns

Dictionary<TNewKey, TValue>

Type Parameters

TKey
TValue
TNewKey

TransformValues<TKey, TValue, TNewValue>(IReadOnlyDictionary<TKey, TValue>, Func<TValue, TNewValue>)

Transforms the values of the source dictionary into new values using the provided key selector function, and constructs a new dictionary with the transformed values and the original keys.

public static Dictionary<TKey, TNewValue> TransformValues<TKey, TValue, TNewValue>(this IReadOnlyDictionary<TKey, TValue> source, Func<TValue, TNewValue> valueSelector) where TKey : notnull

Parameters

source IReadOnlyDictionary<TKey, TValue>
valueSelector Func<TValue, TNewValue>

Returns

Dictionary<TKey, TNewValue>

Type Parameters

TKey
TValue
TNewValue

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

Adds a collection of new entries to the dictionary. For each of the given entries, if its key already exists, its value is preserved in the source dictionary.

public static bool TryAddPreserveRange<TKey, TValue>(this IDictionary<TKey, TValue> source, IDictionary<TKey, TValue> entries) where TKey : notnull

Parameters

source IDictionary<TKey, TValue>

The source dictionary.

entries IDictionary<TKey, TValue>

The entries to add or overwrite.

Returns

bool

true if none of the given entries already existed with a different value, otherwise false.

Type Parameters

TKey

The type of the keys stored in the dictionary.

TValue

The type of the values stored in the dictionary.

TryAddPreserve<TKey, TValue>(IDictionary<TKey, TValue>, KeyValuePair<TKey, TValue>)

Adds a new entry to the dictionary. If the given key already exists, its value is preserved in the source dictionary.

public static bool TryAddPreserve<TKey, TValue>(this IDictionary<TKey, TValue> source, KeyValuePair<TKey, TValue> kvp) where TKey : notnull

Parameters

source IDictionary<TKey, TValue>

The source dictionary.

kvp KeyValuePair<TKey, TValue>

The KeyValuePair<TKey, TValue> to add to the dictionary.

Returns

bool

true if the entry did not exist, or existed with the same value, otherwise false.

Type Parameters

TKey

The type of the keys stored in the dictionary.

TValue

The type of the values stored in the dictionary.

TryAddPreserve<TKey, TValue>(IDictionary<TKey, TValue>, KeyValuePair<TKey, TValue>, out TValue?)

Adds a new entry to the dictionary. If the given key already exists, its value is preserved in the source dictionary.

public static bool TryAddPreserve<TKey, TValue>(this IDictionary<TKey, TValue> source, KeyValuePair<TKey, TValue> kvp, out TValue? existingValue) where TKey : notnull

Parameters

source IDictionary<TKey, TValue>

The source dictionary.

kvp KeyValuePair<TKey, TValue>

The KeyValuePair<TKey, TValue> to add to the dictionary.

existingValue TValue

The value that existed in the dictionary, if the key was already present, otherwise default.

Returns

bool

true if the entry did not exist, or existed with the same value, otherwise false.

Type Parameters

TKey

The type of the keys stored in the dictionary.

TValue

The type of the values stored in the dictionary.

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

Adds a new entry to the dictionary. If the given key already exists, its value is preserved in the source dictionary.

public static bool TryAddPreserve<TKey, TValue>(this IDictionary<TKey, TValue> source, TKey key, TValue value) where TKey : notnull

Parameters

source IDictionary<TKey, TValue>

The source dictionary.

key TKey

The key of the entry to add or overwrite.

value TValue

The value of the entry to set.

Returns

bool

true if the entry did not exist, or existed with the same value, otherwise false.

Type Parameters

TKey

The type of the keys stored in the dictionary.

TValue

The type of the values stored in the dictionary.

TryAddPreserve<TKey, TValue>(IDictionary<TKey, TValue>, TKey, TValue, out TValue?)

Adds a new entry to the dictionary. If the given key already exists, its value is preserved in the source dictionary.

public static bool TryAddPreserve<TKey, TValue>(this IDictionary<TKey, TValue> source, TKey key, TValue value, out TValue? existingValue) where TKey : notnull

Parameters

source IDictionary<TKey, TValue>

The source dictionary.

key TKey

The key of the entry to add or overwrite.

value TValue

The value of the entry to set.

existingValue TValue

The value that existed in the dictionary, if the key was already present, otherwise default.

Returns

bool

true if the entry did not exist, or existed with the same value, otherwise false.

Type Parameters

TKey

The type of the keys stored in the dictionary.

TValue

The type of the values stored in the dictionary.

TryAdd<TKey, TValue>(IDictionary<TKey, TValue>, KeyValuePair<TKey, TValue>)

Attempts to add an entry to the provided dictionary, if the key does not already exist.

public static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> source, KeyValuePair<TKey, TValue> kvp) where TKey : notnull

Parameters

source IDictionary<TKey, TValue>

The source dictionary.

kvp KeyValuePair<TKey, TValue>

The entry to add to the dictionary.

Returns

bool

true if the entry was successfully added, otherwise false.

Type Parameters

TKey

The type of the keys stored in the dictionary.

TValue

The type of the values stored in the dictionary.

ValueOrDefault<TKey, TValue>(IDictionary<TKey, TValue>, TKey?)

Gets the value mapped to the given key within the dictionary, if the key is present.

public static TValue? ValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey? key) where TKey : notnull

Parameters

dictionary IDictionary<TKey, TValue>

The dictionary whose mapped value to get.

key TKey

The key whose mapped value to get.

Returns

TValue

The associated value to key, if it exists, otherwise default.

Type Parameters

TKey

The type of the keys stored in the dictionary.

TValue

The type of the values stored in the dictionary.

ValueOrDefault<TKey, TValue>(IDictionary<TKey, TValue>, TKey?, TValue?)

Gets the value mapped to the given key within the dictionary, if the key is present.

public static TValue? ValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey? key, TValue? defaultValue) where TKey : notnull

Parameters

dictionary IDictionary<TKey, TValue>

The dictionary whose mapped value to get.

key TKey

The key whose mapped value to get.

defaultValue TValue

The default value to return if the key is null or is not found in the dictionary.

Returns

TValue

The associated value to key, if it exists, otherwise the specified default value.

Type Parameters

TKey

The type of the keys stored in the dictionary.

TValue

The type of the values stored in the dictionary.