Class DictionaryEx
Provides static extension methods to augment the functionality of dictionary types.
public static class DictionaryEx
- Inheritance
-
DictionaryEx
- Inherited Members
Methods
GetValue<TK, TV>(IDictionary<TK, TV>, TK, TV?)
Retrieves the value associated with the specified key from the dictionary, or returns the provided default value if the key is not found.
public static TV? GetValue<TK, TV>(this IDictionary<TK, TV> dict, TK key, TV? defaultValue = default)
Parameters
dictIDictionary<TK, TV>The dictionary to search for the key.
keyTKThe key whose associated value should be returned.
defaultValueTVThe value to return when the key is not present in the dictionary. Defaults to the default value of
TV.
Returns
- TV
The value associated with the key if found; otherwise,
defaultValue.
Type Parameters
TKTV
ToListString<TK, TV>(IDictionary<TK, TV>, string, string)
Converts a dictionary into a formatted string by joining each key-value pair with a key separator and concatenating all pairs with an item separator.
public static string ToListString<TK, TV>(this IDictionary<TK, TV> dict, string itemSeparator = ", ", string keySeparator = ": ")
Parameters
dictIDictionary<TK, TV>The source dictionary whose entries will be converted to a string.
itemSeparatorstringThe separator placed between each formatted key-value pair in the resulting string.
keySeparatorstringThe separator placed between a key and its corresponding value within each pair.
Returns
- string
A single string containing all dictionary entries formatted and joined using the specified separators.
Type Parameters
TKTV
TryGetAndReturn<TKey, TValue>(IDictionary<TKey, TValue>, TKey)
Attempts to retrieve the value associated with the specified key from the dictionary, returning null if the key is not found.
public static TValue? TryGetAndReturn<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key) where TKey : notnull where TValue : class
Parameters
dictionaryIDictionary<TKey, TValue>The dictionary to search for the key.
keyTKeyThe key whose associated value should be retrieved.
Returns
- TValue
The value associated with the key, or null if the key was not found in the dictionary.
Type Parameters
TKeyTValue