Search code examples
c#genericsc#-8.0non-nullablenullable-reference-types

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


The post is specific to C# 8. Let's assume I want to have this method:

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

If my .csproj looks like this (i.e. C# 8 and nullable types are enabled, all warnings are errors):

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <LangVersion>8</LangVersion>
    <Nullable>enable</Nullable>
    <WarningsAsErrors>true</WarningsAsErrors>
  </PropertyGroup>
  …
</Project>

This code will produce the following build-time error:

DictionaryEx.cs(28, 78): [CS8714] The type 'TKey' cannot be used as type parameter 'TKey' in the generic type or method 'Dictionary'. Nullability of type argument 'TKey' doesn't match 'notnull' constraint.

Is there any way to specify that TKey has to be a non-nullable type?


Solution

  • 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;