我有一个通用接口IDataAdapter<T>;接口的实现者应该能够Guid从数据源读取带有ID 的POCO 。IDataAdapter<T>有一个Read(Guid id)我想返回 a 的方法T?,其中 null 表示在数据源中找不到匹配项。然而,即使约束T : notnull上IDataAdapter<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)