Let's say we have some generic method with 2 parameters:
private void TestConstraints<TKey>(TKey key, TKey? nullableKey)
where TKey : notnull { }
That's perfectly fine from .NET6 perspective, where not nullable reference types were added. But using attempt is failing with reason CS8714 type can't be used as parameter:
private void TestMethod()
{
var id = 5;
int? nullableId = null;
TestConstraints(id, nullableId);
}
Warning CS8714 The type 'int?' cannot be used as type parameter 'TKey' in the generic type or method 'TestClass.TestConstraints(TKey, TKey?)'. Nullability of type argument 'int?' doesn't match 'notnull' constraint.
Specifying type explicitly as int
does not help.
Could someone clarify if I could operate this way (defining input parameters as TKey?
) or not, and where in documentation it is stated?
Compiler version:
Microsoft (R) Build Engine version 17.2.0+41abc5629