Table of Contents

Class DictionaryEx

Namespace
adas_core.Domain.Utils
Assembly
adas-core.Domain.dll

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

dict IDictionary<TK, TV>

The dictionary to search for the key.

key TK

The key whose associated value should be returned.

defaultValue TV

The 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

TK
TV

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

dict IDictionary<TK, TV>

The source dictionary whose entries will be converted to a string.

itemSeparator string

The separator placed between each formatted key-value pair in the resulting string.

keySeparator string

The 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

TK
TV

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

dictionary IDictionary<TKey, TValue>

The dictionary to search for the key.

key TKey

The 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

TKey
TValue