Class DictionaryExtensions
Extension methods for Dictionary & ConcurrentDictionary.
Inheritance
Namespace: Umbraco.Extensions
Assembly: Umbraco.Core.dll
Syntax
public static class DictionaryExtensions
Methods
View SourceContainsKeyIgnoreCase<TValue>(IDictionary<string, TValue>, string)
contains key ignore case.
Declaration
public static bool ContainsKeyIgnoreCase<TValue>(this IDictionary<string, TValue> dictionary, string key)
Parameters
| Type | Name | Description |
|---|---|---|
| IDictionary<string, TValue> | dictionary | The dictionary. |
| string | key | The key. |
Returns
| Type | Description |
|---|---|
| bool | The contains key ignore case. |
Type Parameters
| Name | Description |
|---|---|
| TValue | Value Type |
ConvertTo<TKeyOut, TValOut>(IDictionary)
Converts a dictionary to another type by only using direct casting
Declaration
public static IDictionary<TKeyOut, TValOut> ConvertTo<TKeyOut, TValOut>(this IDictionary d) where TKeyOut : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| IDictionary | d |
Returns
| Type | Description |
|---|---|
| IDictionary<TKeyOut, TValOut> |
Type Parameters
| Name | Description |
|---|---|
| TKeyOut | |
| TValOut |
ConvertTo<TKeyOut, TValOut>(IDictionary, Func<object, TKeyOut>, Func<object, TValOut>)
Converts a dictionary to another type using the specified converters
Declaration
public static IDictionary<TKeyOut, TValOut> ConvertTo<TKeyOut, TValOut>(this IDictionary d, Func<object, TKeyOut> keyConverter, Func<object, TValOut> valConverter) where TKeyOut : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| IDictionary | d | |
| Func<object, TKeyOut> | keyConverter | |
| Func<object, TValOut> | valConverter |
Returns
| Type | Description |
|---|---|
| IDictionary<TKeyOut, TValOut> |
Type Parameters
| Name | Description |
|---|---|
| TKeyOut | |
| TValOut |
GetOrCreate<TKey, TVal>(IDictionary<TKey, TVal>, TKey)
Method to Get a value by the key. If the key doesn't exist it will create a new TVal object for the key and return it.
Declaration
public static TVal GetOrCreate<TKey, TVal>(this IDictionary<TKey, TVal> dict, TKey key) where TVal : class, new()
Parameters
| Type | Name | Description |
|---|---|---|
| IDictionary<TKey, TVal> | dict | |
| TKey | key |
Returns
| Type | Description |
|---|---|
| TVal |
Type Parameters
| Name | Description |
|---|---|
| TKey | |
| TVal |
GetValueAsString<TKey, TVal>(IDictionary<TKey, TVal>, TKey)
Returns the value of the key value based on the key as it's string value, if the key is not found, then an empty string is returned
Declaration
public static string? GetValueAsString<TKey, TVal>(this IDictionary<TKey, TVal> d, TKey key)
Parameters
| Type | Name | Description |
|---|---|---|
| IDictionary<TKey, TVal> | d | |
| TKey | key |
Returns
| Type | Description |
|---|---|
| string |
Type Parameters
| Name | Description |
|---|---|
| TKey | |
| TVal |
GetValueAsString<TKey, TVal>(IDictionary<TKey, TVal>, TKey, string)
Returns the value of the key value based on the key as it's string value, if the key is not found or is an empty string, then the provided default value is returned
Declaration
public static string? GetValueAsString<TKey, TVal>(this IDictionary<TKey, TVal> d, TKey key, string defaultValue)
Parameters
| Type | Name | Description |
|---|---|---|
| IDictionary<TKey, TVal> | d | |
| TKey | key | |
| string | defaultValue |
Returns
| Type | Description |
|---|---|
| string |
Type Parameters
| Name | Description |
|---|---|
| TKey | |
| TVal |
GetValueIgnoreCase<TValue>(IDictionary<string, TValue>, string)
The get entry ignore case.
Declaration
public static TValue? GetValueIgnoreCase<TValue>(this IDictionary<string, TValue> dictionary, string key)
Parameters
| Type | Name | Description |
|---|---|---|
| IDictionary<string, TValue> | dictionary | The dictionary. |
| string | key | The key. |
Returns
| Type | Description |
|---|---|
| TValue | The entry |
Type Parameters
| Name | Description |
|---|---|
| TValue | The type |
GetValueIgnoreCase<TValue>(IDictionary<string, TValue>, string?, TValue)
The get entry ignore case.
Declaration
public static TValue GetValueIgnoreCase<TValue>(this IDictionary<string, TValue> dictionary, string? key, TValue defaultValue)
Parameters
| Type | Name | Description |
|---|---|---|
| IDictionary<string, TValue> | dictionary | The dictionary. |
| string | key | The key. |
| TValue | defaultValue | The default value. |
Returns
| Type | Description |
|---|---|
| TValue | The entry |
Type Parameters
| Name | Description |
|---|---|
| TValue | The type |
GetValue<TKey, TVal>(IDictionary<TKey, TVal>, TKey, TVal?)
Returns the value of the key value based on the key, if the key is not found, a null value is returned
Declaration
public static TVal? GetValue<TKey, TVal>(this IDictionary<TKey, TVal> d, TKey key, TVal? defaultValue = default)
Parameters
| Type | Name | Description |
|---|---|---|
| IDictionary<TKey, TVal> | d | The d. |
| TKey | key | The key. |
| TVal | defaultValue | The default value. |
Returns
| Type | Description |
|---|---|
| TVal |
Type Parameters
| Name | Description |
|---|---|
| TKey | The type of the key. |
| TVal | The type of the val. |
MergeLeft<T, TK, TV>(T, IDictionary<TK, TV>, bool)
Merges all key/values from the sources dictionaries into the destination dictionary
Declaration
public static void MergeLeft<T, TK, TV>(this T destination, IDictionary<TK, TV> source, bool overwrite = false) where T : IDictionary<TK, TV>
Parameters
| Type | Name | Description |
|---|---|---|
| T | destination | The source dictionary to merge other dictionaries into |
| IDictionary<TK, TV> | source | The other dictionary to merge values from |
| bool | overwrite | By default all values will be retained in the destination if the same keys exist in the sources but this can changed if overwrite = true, then any key/value found in any of the sources will overwritten in the destination. Note that it will just use the last found key/value if this is true. |
Type Parameters
| Name | Description |
|---|---|
| T | |
| TK | |
| TV |
MergeLeft<T, TK, TV>(T, IEnumerable<IDictionary<TK, TV>>, bool)
Merges all key/values from the sources dictionaries into the destination dictionary
Declaration
public static void MergeLeft<T, TK, TV>(this T destination, IEnumerable<IDictionary<TK, TV>> sources, bool overwrite = false) where T : IDictionary<TK, TV>
Parameters
| Type | Name | Description |
|---|---|---|
| T | destination | The source dictionary to merge other dictionaries into |
| IEnumerable<IDictionary<TK, TV>> | sources | The other dictionaries to merge values from |
| bool | overwrite | By default all values will be retained in the destination if the same keys exist in the sources but this can changed if overwrite = true, then any key/value found in any of the sources will overwritten in the destination. Note that it will just use the last found key/value if this is true. |
Type Parameters
| Name | Description |
|---|---|
| T | |
| TK | |
| TV |
ToDictionaryAsync<TInput, TKey, TValue>(IEnumerable<TInput>, Func<TInput, TKey>, Func<TInput, Task<TValue>>)
Asynchronously converts an enumerable to a dictionary using an async value selector.
Declaration
public static Task<Dictionary<TKey, TValue>> ToDictionaryAsync<TInput, TKey, TValue>(this IEnumerable<TInput> enumerable, Func<TInput, TKey> syncKeySelector, Func<TInput, Task<TValue>> asyncValueSelector) where TKey : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TInput> | enumerable | The source enumerable. |
| Func<TInput, TKey> | syncKeySelector | A function to extract the key from each element. |
| Func<TInput, Task<TValue>> | asyncValueSelector | An async function to extract the value from each element. |
Returns
| Type | Description |
|---|---|
| Task<Dictionary<TKey, TValue>> | A task that represents the asynchronous operation, containing the resulting dictionary. |
Type Parameters
| Name | Description |
|---|---|
| TInput | The type of the input elements. |
| TKey | The type of the dictionary keys. |
| TValue | The type of the dictionary values. |
ToNameValueCollection(IDictionary<string, string>)
Converts a dictionary to a NameValueCollection
Declaration
public static NameValueCollection ToNameValueCollection(this IDictionary<string, string> d)
Parameters
| Type | Name | Description |
|---|---|---|
| IDictionary<string, string> | d |
Returns
| Type | Description |
|---|---|
| NameValueCollection |
ToQueryString(IDictionary<string, object?>)
Converts a dictionary object to a query string representation such as: firstname=shannon&lastname=deminick.
Declaration
public static string ToQueryString(this IDictionary<string, object?> d)
Parameters
| Type | Name | Description |
|---|---|---|
| IDictionary<string, object> | d |
Returns
| Type | Description |
|---|---|
| string |
TryUpdateOptimisitic<TKey, TValue>(ConcurrentDictionary<TKey, TValue>, TKey, Func<TValue, TValue>)
Updates an item with the specified key with the specified value
Declaration
public static bool TryUpdateOptimisitic<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dict, TKey key, Func<TValue, TValue> updateFactory) where TKey : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| ConcurrentDictionary<TKey, TValue> | dict | |
| TKey | key | |
| Func<TValue, TValue> | updateFactory |
Returns
| Type | Description |
|---|---|
| bool |
Type Parameters
| Name | Description |
|---|---|
| TKey | |
| TValue |
Remarks
Taken from: http://stackoverflow.com/questions/12240219/is-there-a-way-to-use-concurrentdictionary-tryupdate-with-a-lambda-expression WARNING: If the value changes after we've retrieved it, then the item will not be updated
TryUpdate<TKey, TValue>(ConcurrentDictionary<TKey, TValue>, TKey, Func<TValue, TValue>)
Updates an item with the specified key with the specified value
Declaration
public static bool TryUpdate<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dict, TKey key, Func<TValue, TValue> updateFactory) where TKey : notnull
Parameters
| Type | Name | Description |
|---|---|---|
| ConcurrentDictionary<TKey, TValue> | dict | |
| TKey | key | |
| Func<TValue, TValue> | updateFactory |
Returns
| Type | Description |
|---|---|
| bool |
Type Parameters
| Name | Description |
|---|---|
| TKey | |
| TValue |
Remarks
Taken from: http://stackoverflow.com/questions/12240219/is-there-a-way-to-use-concurrentdictionary-tryupdate-with-a-lambda-expression If there is an item in the dictionary with the key, it will keep trying to update it until it can