Class Dictionary
Represents various extensions for types Dictionary<TKey,TValue> and IDictionary<TKey,TValue>.
Inherited Members
Namespace: DotNext.Collections.Generic
Assembly: DotNext.dll
Syntax
public static class Dictionary
Methods
| Improve this Doc View SourceConvertValues<TKey, TValue, TResult>(IReadOnlyDictionary<TKey, TValue>, ValueFunc<TValue, TResult>)
Applies lazy conversion for each dictionary value.
Declaration
public static ReadOnlyDictionaryView<TKey, TValue, TResult> ConvertValues<TKey, TValue, TResult>(this IReadOnlyDictionary<TKey, TValue> dictionary, in ValueFunc<TValue, TResult> mapper)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyDictionary<TKey, TValue> | dictionary | A dictionary to be mapped. |
ValueFunc<TValue, TResult> | mapper | Mapping function. |
Returns
Type | Description |
---|---|
ReadOnlyDictionaryView<TKey, TValue, TResult> | Read-only view of the dictionary where each value is converted in lazy manner. |
Type Parameters
Name | Description |
---|---|
TKey | Type of keys. |
TValue | Type of values. |
TResult | Type of mapped values. |
ConvertValues<TKey, TValue, TResult>(IReadOnlyDictionary<TKey, TValue>, Converter<TValue, TResult>)
Applies lazy conversion for each dictionary value.
Declaration
public static ReadOnlyDictionaryView<TKey, TValue, TResult> ConvertValues<TKey, TValue, TResult>(this IReadOnlyDictionary<TKey, TValue> dictionary, Converter<TValue, TResult> mapper)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyDictionary<TKey, TValue> | dictionary | A dictionary to be mapped. |
Converter<TValue, TResult> | mapper | Mapping function. |
Returns
Type | Description |
---|---|
ReadOnlyDictionaryView<TKey, TValue, TResult> | Read-only view of the dictionary where each value is converted in lazy manner. |
Type Parameters
Name | Description |
---|---|
TKey | Type of keys. |
TValue | Type of values. |
TResult | Type of mapped values. |
ForEach<TKey, TValue>(IDictionary<TKey, TValue>, ValueAction<TKey, TValue>)
Applies specific action to each dictionary.
Declaration
public static void ForEach<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, in ValueAction<TKey, TValue> action)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | dictionary | A dictionary to read from. |
ValueAction<TKey, TValue> | action | The action to be applied for each key/value pair. |
Type Parameters
Name | Description |
---|---|
TKey | The key type of the dictionary. |
TValue | The value type of the dictionary. |
ForEach<TKey, TValue>(IDictionary<TKey, TValue>, Action<TKey, TValue>)
Applies specific action to each dictionary.
Declaration
public static void ForEach<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, Action<TKey, TValue> action)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | dictionary | A dictionary to read from. |
Action<TKey, TValue> | action | The action to be applied for each key/value pair. |
Type Parameters
Name | Description |
---|---|
TKey | The key type of the dictionary. |
TValue | The value type of the dictionary. |
GetOrAdd<TKey, TValue>(Dictionary<TKey, TValue>, TKey)
Adds a key-value pair to the dictionary if the key does not exist.
Declaration
public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key)
where TValue : new()
Parameters
Type | Name | Description |
---|---|---|
Dictionary<TKey, TValue> | dictionary | The source dictionary. |
TKey | key | The key of the key-value pair. |
Returns
Type | Description |
---|---|
TValue | The corresponding value in the dictionary if |
Type Parameters
Name | Description |
---|---|
TKey | The key type of the dictionary. |
TValue | The value type of the dictionary. |
GetOrAdd<TKey, TValue>(Dictionary<TKey, TValue>, TKey, TValue)
Adds a key-value pair to the dictionary if the key does not exist.
Declaration
public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value)
Parameters
Type | Name | Description |
---|---|---|
Dictionary<TKey, TValue> | dictionary | The source dictionary. |
TKey | key | The key of the key-value pair. |
TValue | value | The value of the key-value pair. |
Returns
Type | Description |
---|---|
TValue | The corresponding value in the dictionary if |
Type Parameters
Name | Description |
---|---|
TKey | The key type of the dictionary. |
TValue | The value type of the dictionary. |
GetOrAdd<TKey, TValue>(Dictionary<TKey, TValue>, TKey, ValueFunc<TKey, TValue>)
Generates a value and adds the key-value pair to the dictionary if the key does not exist.
Declaration
public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, in ValueFunc<TKey, TValue> valueFactory)
Parameters
Type | Name | Description |
---|---|---|
Dictionary<TKey, TValue> | dictionary | The source dictionary. |
TKey | key | The key of the key-value pair. |
ValueFunc<TKey, TValue> | valueFactory | The function used to generate the value from the key. |
Returns
Type | Description |
---|---|
TValue | The corresponding value in the dictionary if |
Type Parameters
Name | Description |
---|---|
TKey | The key type of the dictionary. |
TValue | The value type of the dictionary. |
GetOrAdd<TKey, TValue>(Dictionary<TKey, TValue>, TKey, Func<TKey, TValue>)
Generates a value and adds the key-value pair to the dictionary if the key does not exist.
Declaration
public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> valueFactory)
Parameters
Type | Name | Description |
---|---|---|
Dictionary<TKey, TValue> | dictionary | The source dictionary. |
TKey | key | The key of the key-value pair. |
Func<TKey, TValue> | valueFactory | The function used to generate the value from the key. |
Returns
Type | Description |
---|---|
TValue | The corresponding value in the dictionary if |
Type Parameters
Name | Description |
---|---|
TKey | The key type of the dictionary. |
TValue | The value type of the dictionary. |
GetOrInvoke<TKey, TValue>(IDictionary<TKey, TValue>, TKey, ValueFunc<TValue>)
Gets dictionary value by key if it exists or
invoke defaultValue
and
return its result as a default value.
Declaration
public static TValue GetOrInvoke<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, in ValueFunc<TValue> defaultValue)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | dictionary | A dictionary to read from. |
TKey | key | A key associated with the value. |
ValueFunc<TValue> | defaultValue | A delegate to be invoked if key doesn't exist in the dictionary. |
Returns
Type | Description |
---|---|
TValue | The value associated with the key or returned by the delegate. |
Type Parameters
Name | Description |
---|---|
TKey | Type of dictionary keys. |
TValue | Type of dictionary values. |
GetOrInvoke<TKey, TValue>(IDictionary<TKey, TValue>, TKey, Func<TValue>)
Gets dictionary value by key if it exists or
invoke defaultValue
and
return its result as a default value.
Declaration
public static TValue GetOrInvoke<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TValue> defaultValue)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | dictionary | A dictionary to read from. |
TKey | key | A key associated with the value. |
Func<TValue> | defaultValue | A delegate to be invoked if key doesn't exist in the dictionary. |
Returns
Type | Description |
---|---|
TValue | The value associated with the key or returned by the delegate. |
Type Parameters
Name | Description |
---|---|
TKey | Type of dictionary keys. |
TValue | Type of dictionary values. |
IndexerGetter<TKey, TValue>(IDictionary<TKey, TValue>)
Returns System.Collections.Generic.IDictionary`2.get_Item(`0) as delegate attached to the dictionary instance.
Declaration
public static Func<TKey, TValue> IndexerGetter<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | dictionary | Mutable dictionary instance. |
Returns
Type | Description |
---|---|
Func<TKey, TValue> | A delegate representing dictionary indexer. |
Type Parameters
Name | Description |
---|---|
TKey | Type of dictionary keys. |
TValue | Type of dictionary values. |
IndexerGetter<TKey, TValue>(IReadOnlyDictionary<TKey, TValue>)
Returns System.Collections.Generic.IReadOnlyDictionary`2.get_Item(`0) as delegate attached to the dictionary instance.
Declaration
public static Func<TKey, TValue> IndexerGetter<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyDictionary<TKey, TValue> | dictionary | Read-only dictionary instance. |
Returns
Type | Description |
---|---|
Func<TKey, TValue> | A delegate representing dictionary indexer. |
Type Parameters
Name | Description |
---|---|
TKey | Type of dictionary keys. |
TValue | Type of dictionary values. |
IndexerSetter<TKey, TValue>(IDictionary<TKey, TValue>)
Returns System.Collections.Generic.IDictionary`2.set_Item(`0,`1) as delegate attached to the dictionary instance.
Declaration
public static Action<TKey, TValue> IndexerSetter<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | dictionary | Mutable dictionary instance. |
Returns
Type | Description |
---|---|
Action<TKey, TValue> | A delegate representing dictionary indexer. |
Type Parameters
Name | Description |
---|---|
TKey | Type of dictionary keys. |
TValue | Type of dictionary values. |
KeysGetter<TKey, TValue>(IDictionary<TKey, TValue>)
Returns Keys as delegate attached to the dictionary instance.
Declaration
public static Func<ICollection<TKey>> KeysGetter<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | dictionary | Read-only dictionary instance. |
Returns
Type | Description |
---|---|
Func<ICollection<TKey>> | A delegate providing access to dictionary keys. |
Type Parameters
Name | Description |
---|---|
TKey | Type of dictionary keys. |
TValue | Type of dictionary values. |
KeysGetter<TKey, TValue>(IReadOnlyDictionary<TKey, TValue>)
Returns Keys as delegate attached to the dictionary instance.
Declaration
public static Func<IEnumerable<TKey>> KeysGetter<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyDictionary<TKey, TValue> | dictionary | Read-only dictionary instance. |
Returns
Type | Description |
---|---|
Func<IEnumerable<TKey>> | A delegate providing access to dictionary keys. |
Type Parameters
Name | Description |
---|---|
TKey | Type of dictionary keys. |
TValue | Type of dictionary values. |
TryGetValue<TKey, TValue>(IDictionary<TKey, TValue>, TKey)
Gets the value associated with the specified key.
Declaration
public static Optional<TValue> TryGetValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | dictionary | A dictionary to read from. |
TKey | key | The key whose value to get. |
Returns
Type | Description |
---|---|
Optional<TValue> | The optional value associated with the key. |
Type Parameters
Name | Description |
---|---|
TKey | Type of dictionary keys. |
TValue | Type of dictionary values. |
TryGetValue<TKey, TValue>(IReadOnlyDictionary<TKey, TValue>, TKey)
Gets the value associated with the specified key.
Declaration
public static Optional<TValue> TryGetValue<TKey, TValue>(IReadOnlyDictionary<TKey, TValue> dictionary, TKey key)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyDictionary<TKey, TValue> | dictionary | A dictionary to read from. |
TKey | key | The key whose value to get. |
Returns
Type | Description |
---|---|
Optional<TValue> | The optional value associated with the key. |
Type Parameters
Name | Description |
---|---|
TKey | Type of dictionary keys. |
TValue | Type of dictionary values. |
TryRemove<TKey, TValue>(IDictionary<TKey, TValue>, TKey)
Removes the value with the specified key and return the removed value.
Declaration
public static Optional<TValue> TryRemove<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | dictionary | A dictionary to modify. |
TKey | key | The key of the element to remove. |
Returns
Type | Description |
---|---|
Optional<TValue> | The removed value. |
Type Parameters
Name | Description |
---|---|
TKey | Type of dictionary keys. |
TValue | Type of dictionary values. |
ValuesGetter<TKey, TValue>(IDictionary<TKey, TValue>)
Returns Values as delegate attached to the dictionary instance.
Declaration
public static Func<ICollection<TValue>> ValuesGetter<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | dictionary | Read-only dictionary instance. |
Returns
Type | Description |
---|---|
Func<ICollection<TValue>> | A delegate providing access to dictionary keys. |
Type Parameters
Name | Description |
---|---|
TKey | Type of dictionary keys. |
TValue | Type of dictionary values. |
ValuesGetter<TKey, TValue>(IReadOnlyDictionary<TKey, TValue>)
Returns Values as delegate attached to the dictionary instance.
Declaration
public static Func<IEnumerable<TValue>> ValuesGetter<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyDictionary<TKey, TValue> | dictionary | Read-only dictionary instance. |
Returns
Type | Description |
---|---|
Func<IEnumerable<TValue>> | A delegate providing access to dictionary keys. |
Type Parameters
Name | Description |
---|---|
TKey | Type of dictionary keys. |
TValue | Type of dictionary values. |