public static class DictionaryExtensions
{
public static T GetKey<T, K>(this IDictionary<K, T> dict, K key, T defValue = default) where K : struct
{
return dict.TryGetValue(key, out T value) ? value : defValue;
}
public static T GetKey<T, K>(this IDictionary<K?, T> dict, K? key, T defValue = default) where K : struct
{
return key.HasValue && dict.TryGetValue(key, out T value) ? value : defValue;
}
public static T GetKey<T, K>(this IDictionary<K, T> dict, K? key, T defValue = default) where K : struct
{
return key.HasValue && dict.TryGetValue(key.Value, out T value) ? value : defValue;
}
public static T GetKeyC<T, K>(this IDictionary<K, T> dict, K key, T defValue = default) where K : class
{
return key != null && dict.TryGetValue(key, out T value) ? value : defValue;
}
}374600cookie-checkC# Dictionary GetKey