Method TryGetValue
- Namespace
- SodaFlow.Functional
- Assembly
- SodaFlow.Functional.dll
TryGetValue<TKey, TValue>(IReadOnlyDictionary<TKey, TValue>?, TKey)
Gives the value stored for a key, if there is one.
[Pure]
public static Maybe<TValue> TryGetValue<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue>? dictionary, TKey key)
Parameters
dictionaryIReadOnlyDictionary<TKey, TValue>The dictionary to look in. A null dictionary counts as empty.
keyTKeyThe key to look up.
Returns
- Maybe<TValue>
A Maybe<T> containing the value stored for
key, and one containing no value if the dictionary has no entry for it.
Type Parameters
TKeyThe type of the keys in the dictionary.
TValueThe type of the values in the dictionary.
Remarks
The same lookup as TryGetValue(TKey, out TValue), without the output parameter. It also removes the ambiguity where a missing key and a stored default each give the default for the value type. This code gives a null key to the dictionary as it is, thus this throws for the implementations which reject one. That is a mistake in the calling code rather than a missing entry, and is not something to answer with no value.