Method SafeGet
SafeGet<TKey, TValue>(Dictionary<TKey, TValue>, TKey, bool)
Safe version of Dictionary [] operator, which will optionally add element if target elements does not exist based on autoAdd
Returns the default value of the value type if the key does not exist and the autoAdd
parameter is false.
Declaration
public static TValue SafeGet<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, bool autoAdd = true) where TValue : new()
Parameters
Type | Name | Description |
---|---|---|
Dictionary<TKey, TValue> | dictionary | The dictionary to get a value from. |
TKey | key | The key to get the value for. |
bool | autoAdd | Whether to automatically add a default value to the dictionary if the key is not already present. Default is true. |
Returns
Type | Description |
---|---|
TValue | The value associated with the specified key if the key is found, otherwise the default value of the value type. |
Type Parameters
Name | Description |
---|---|
TKey | The type of the dictionary keys. |
TValue | The type of the dictionary values. |