【发布时间】:2020-02-02 09:38:16
【问题描述】:
我有一个通用接口IDataAdapter<T>;接口的实现者应该能够从数据源读取具有Guid ID 的 POCO。 IDataAdapter<T> 有一个方法 Read(Guid id),我想返回一个 T?,其中 null 表示在数据源中找不到匹配项。但是,即使在 IDataAdapter<T> 上使用约束 T : notnull,尝试定义此方法也会出现错误 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
}
【问题讨论】:
-
你用的是什么版本?网络核心 3.0 还是网络标准 2.1?
-
@keysl 核心 3.0.
-
@Manvinder Singh 我不认为这是重复的;该问题/答案早于 c#8 的可为空引用类型。
标签: c# nullable c#-8.0 nullable-reference-types