Add more `GetValueOrDefault` overloads

This commit is contained in:
YoshiRulz 2024-08-23 02:15:24 +10:00
parent 72b2e78957
commit 34d71e90bc
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
1 changed files with 8 additions and 0 deletions

View File

@ -153,6 +153,10 @@ namespace BizHawk.Common.CollectionExtensions
/// If the key is not present, returns default(TValue).
/// backported from .NET Core 2.0
/// </summary>
public static TValue? GetValueOrDefault<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TKey key)
=> dictionary.TryGetValue(key, out var found) ? found : default;
/// <inheritdoc cref="GetValueOrDefault{K,V}(IDictionary{K,V},K)"/>
public static TValue? GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key)
=> dictionary.TryGetValue(key, out var found) ? found : default;
@ -161,6 +165,10 @@ namespace BizHawk.Common.CollectionExtensions
/// If the key is not present, returns <paramref name="defaultValue"/>.
/// backported from .NET Core 2.0
/// </summary>
public static TValue? GetValueOrDefault<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
=> dictionary.TryGetValue(key, out var found) ? found : defaultValue;
/// <inheritdoc cref="GetValueOrDefault{K,V}(IDictionary{K,V},K,V)"/>
public static TValue? GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
=> dictionary.TryGetValue(key, out var found) ? found : defaultValue;
#endif