相关疑难解决方法(0)

即使参数具有非空约束,也会收到有关可为空类型参数的错误

我有一个通用接口IDataAdapter<T>;接口的实现者应该能够Guid从数据源读取带有ID 的POCO 。IDataAdapter<T>有一个Read(Guid id)我想返回 a 的方法T?,其中 null 表示在数据源中找不到匹配项。然而,即使约束T : notnullIDataAdapter<T>,试图定义该方法给出了错误CS8627: A nullable type parameter must be known to be a value type or non-nullable reference type. Consider adding a 'class', 'struct', or type constraint.为什么我仍然收到此错误,甚至T限制到notnull

代码(应该在 C# 8 环境中<Nullable>enable</Nullable>):

interface IDataAdapter<T> where T : notnull
{
    T? Read (Guid id); // error CS8627
}
Run Code Online (Sandbox Code Playgroud)

c# nullable c#-8.0 nullable-reference-types

8
推荐指数
1
解决办法
2290
查看次数

标签 统计

c# ×1

c#-8.0 ×1

nullable ×1

nullable-reference-types ×1