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; } }
374600cookie-checkC# Dictionary GetKey