Class DictionaryExtensions
Provides extension methods for working with Dictionary<TKey, TValue> and ConcurrentDictionary<TKey, TValue> collections.
Inherited Members
Namespace: mk.helpers
Assembly: mk.helpers.dll
Syntax
public static class DictionaryExtensions
Methods
IncrementAt<T>(IDictionary<T, int>, T)
Increments the value at a specified key in a IDictionary<TKey, TValue>.
Declaration
public static void IncrementAt<T>(this IDictionary<T, int> dictionary, T index)
Parameters
| Type | Name | Description |
|---|---|---|
| IDictionary<T, int> | dictionary | The dictionary. |
| T | index | The key to increment. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of keys in the dictionary. |
ToConcurrentBag<TSource>(IEnumerable<TSource>)
Converts an IEnumerable<T> to a ConcurrentBag<T>.
Declaration
public static ConcurrentBag<TSource> ToConcurrentBag<TSource>(this IEnumerable<TSource> source)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | The source collection. |
Returns
| Type | Description |
|---|---|
| ConcurrentBag<TSource> | A ConcurrentBag<T> containing the elements of the source collection. |
Type Parameters
| Name | Description |
|---|---|
| TSource | The type of elements in the source collection. |
ToConcurrentDictionary<TSource, TKey, TElement>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TSource, TElement>)
Converts an IEnumerable<T> to a ConcurrentDictionary<TKey, TValue>.
Declaration
public static ConcurrentDictionary<TKey, TElement> ToConcurrentDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | The source collection. |
| Func<TSource, TKey> | keySelector | A function to extract keys from elements. |
| Func<TSource, TElement> | elementSelector | A function to extract values from elements. |
Returns
| Type | Description |
|---|---|
| ConcurrentDictionary<TKey, TElement> | A ConcurrentDictionary<TKey, TValue> containing the elements of the source collection. |
Type Parameters
| Name | Description |
|---|---|
| TSource | The type of elements in the source collection. |
| TKey | The type of the dictionary keys. |
| TElement | The type of the dictionary values. |
ToConcurrentDictionary<TSource, TKey, TElement>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TSource, TElement>, IEqualityComparer<TKey>)
Converts an IEnumerable<T> to a ConcurrentDictionary<TKey, TValue> using a specified comparer.
Declaration
public static ConcurrentDictionary<TKey, TElement> ToConcurrentDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | The source collection. |
| Func<TSource, TKey> | keySelector | A function to extract keys from elements. |
| Func<TSource, TElement> | elementSelector | A function to extract values from elements. |
| IEqualityComparer<TKey> | comparer | An equality comparer to compare keys. |
Returns
| Type | Description |
|---|---|
| ConcurrentDictionary<TKey, TElement> | A ConcurrentDictionary<TKey, TValue> containing the elements of the source collection. |
Type Parameters
| Name | Description |
|---|---|
| TSource | The type of elements in the source collection. |
| TKey | The type of the dictionary keys. |
| TElement | The type of the dictionary values. |
TryGet<TKey, TValue>(IDictionary<TKey, TValue>, TKey)
Tries to get a value from a IDictionary<TKey, TValue> based on a key.
Declaration
public static TValue TryGet<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
Parameters
| Type | Name | Description |
|---|---|---|
| IDictionary<TKey, TValue> | dictionary | The dictionary. |
| TKey | key | The key to look up. |
Returns
| Type | Description |
|---|---|
| TValue | The value associated with the key, or the default value if not found. |
Type Parameters
| Name | Description |
|---|---|
| TKey | The type of keys in the dictionary. |
| TValue | The type of values in the dictionary. |
TryGet<TKey, TValue>(IDictionary<TKey, TValue>, TKey, TValue)
Tries to get a value from a IDictionary<TKey, TValue> based on a key.
Declaration
public static TValue TryGet<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
Parameters
| Type | Name | Description |
|---|---|---|
| IDictionary<TKey, TValue> | dictionary | The dictionary. |
| TKey | key | The key to look up. |
| TValue | defaultValue | Default value if key is not found |
Returns
| Type | Description |
|---|---|
| TValue | The value associated with the key, or the default value if not found. |
Type Parameters
| Name | Description |
|---|---|
| TKey | The type of keys in the dictionary. |
| TValue | The type of values in the dictionary. |