C# Dictionary GetKey

Date: 2020-05-14
public static class DictionaryExtensions
{
    public static T GetKey<T, K>(this Dictionary<K, T> dict, K? key, T defValue = default)
        where K : struct
    {
        if (key == null)
            return defValue;
        return dict.TryGetValue(key.Value, out T value) ? value : defValue;
    }

    public static T GetKey<T, K>(this Dictionary<K, T> dict, K key, T defValue = default)
        where K : class
    {
        if (key == null)
            return defValue;
        return dict.TryGetValue(key, out T value) ? value : defValue;
    }
}
37460cookie-checkC# Dictionary GetKey