diff --git a/src/BizHawk.Common/Extensions/CollectionExtensions.cs b/src/BizHawk.Common/Extensions/CollectionExtensions.cs index 5af174e14d..8159f09ab7 100644 --- a/src/BizHawk.Common/Extensions/CollectionExtensions.cs +++ b/src/BizHawk.Common/Extensions/CollectionExtensions.cs @@ -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