How do I specify “any non-nullable type” as a generic type parameter constraint?

Ok, just found out that you can use notnull constraint:

public static TValue Get<TKey, TValue>(
    this Dictionary<TKey, TValue> src, 
    TKey key, TValue @default)
    where TKey : notnull
    => src.TryGetValue(key, out var value) ? value : @default;

Leave a Comment