C# Dictionary GetKey

public static class DictionaryExtensions
{
    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 GetKey<T, K>(this IDictionary<K, T> dict, K key, T defValue = default)
    {
        return key != null && dict.TryGetValue(key, out T value) ? value : defValue;
    }
}

// for nullable-guid keys in dotnet 8:
public static class DictionaryExtensions
{
	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 var 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 var value) ? value : defValue;
	}

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