Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 562
Alex Yakunin Avatar asked Aug 03 '19 04:08

Alex Yakunin


1 Answers

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;
like image 175
Alex Yakunin Avatar answered Sep 19 '22 05:09

Alex Yakunin