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
sourceIDictionary<TKey, TValue>The source dictionary.
entriesIDictionary<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
TKeyThe type of the keys stored in the dictionary.
TValueThe 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
sourceIDictionary<TKey, TValue>The source dictionary.
keyTKeyThe key of the entry to add or overwrite.
valueTValueThe value of the entry to set.
Returns
Type Parameters
TKeyThe type of the keys stored in the dictionary.
TValueThe 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
sourceIDictionary<TKey, TValue>The source dictionary.
entriesIDictionary<TKey, TValue>The entries to add to the
sourcedictionary.
Type Parameters
TKeyThe type of the keys stored in the dictionary.
TValueThe 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
sourceIDictionary<TKey, TValue>The source dictionary.
kvpKeyValuePair<TKey, TValue>The entry to add to the dictionary.
Type Parameters
TKeyThe type of the keys stored in the dictionary.
TValueThe 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
sourceIDictionary<TKey, TValue>The source dictionary.
keyTKeyThe 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
TKeyThe type of the keys stored in the dictionary.
TValueThe 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
dIDictionary<TKey, int>The dictionary whose value to increment for the specified key.
keyTKeyThe key in the dictionary whose value to increment.
Type Parameters
TKeyThe 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
sourceIReadOnlyDictionary<TKey, TValue>keySelectorFunc<TKey, TNewKey>
Returns
- Dictionary<TNewKey, TValue>
Type Parameters
TKeyTValueTNewKey
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
sourceIReadOnlyDictionary<TKey, TValue>valueSelectorFunc<TValue, TNewValue>
Returns
- Dictionary<TKey, TNewValue>
Type Parameters
TKeyTValueTNewValue
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
sourceIDictionary<TKey, TValue>The source dictionary.
entriesIDictionary<TKey, TValue>The entries to add or overwrite.
Returns
Type Parameters
TKeyThe type of the keys stored in the dictionary.
TValueThe 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
sourceIDictionary<TKey, TValue>The source dictionary.
kvpKeyValuePair<TKey, TValue>The KeyValuePair<TKey, TValue> to add to the dictionary.
Returns
Type Parameters
TKeyThe type of the keys stored in the dictionary.
TValueThe 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
sourceIDictionary<TKey, TValue>The source dictionary.
kvpKeyValuePair<TKey, TValue>The KeyValuePair<TKey, TValue> to add to the dictionary.
existingValueTValueThe value that existed in the dictionary, if the key was already present, otherwise default.
Returns
Type Parameters
TKeyThe type of the keys stored in the dictionary.
TValueThe 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
sourceIDictionary<TKey, TValue>The source dictionary.
keyTKeyThe key of the entry to add or overwrite.
valueTValueThe value of the entry to set.
Returns
Type Parameters
TKeyThe type of the keys stored in the dictionary.
TValueThe 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
sourceIDictionary<TKey, TValue>The source dictionary.
keyTKeyThe key of the entry to add or overwrite.
valueTValueThe value of the entry to set.
existingValueTValueThe value that existed in the dictionary, if the key was already present, otherwise default.
Returns
Type Parameters
TKeyThe type of the keys stored in the dictionary.
TValueThe 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
sourceIDictionary<TKey, TValue>The source dictionary.
kvpKeyValuePair<TKey, TValue>The entry to add to the dictionary.
Returns
Type Parameters
TKeyThe type of the keys stored in the dictionary.
TValueThe 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
dictionaryIDictionary<TKey, TValue>The dictionary whose mapped value to get.
keyTKeyThe key whose mapped value to get.
Returns
- TValue
The associated value to
key, if it exists, otherwise default.
Type Parameters
TKeyThe type of the keys stored in the dictionary.
TValueThe 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
dictionaryIDictionary<TKey, TValue>The dictionary whose mapped value to get.
keyTKeyThe key whose mapped value to get.
defaultValueTValueThe 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
TKeyThe type of the keys stored in the dictionary.
TValueThe type of the values stored in the dictionary.