Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C“泛型错误-”的帮助;类型';T';必须是不可为空的值类型";_C#_Generics - Fatal编程技术网

C# C“泛型错误-”的帮助;类型';T';必须是不可为空的值类型";

C# C“泛型错误-”的帮助;类型';T';必须是不可为空的值类型";,c#,generics,C#,Generics,我是C#新手,不明白为什么下面的代码不起作用 public static Nullable<T> CoalesceMax<T>(Nullable<T> a, Nullable<T> b) where T : IComparable { if (a.HasValue && b.HasValue) return a.Value.CompareTo(b.Value) < 0 ? b : a; else

我是C#新手,不明白为什么下面的代码不起作用

public static Nullable<T> CoalesceMax<T>(Nullable<T> a, Nullable<T> b) where T : IComparable
{
    if (a.HasValue && b.HasValue)
        return a.Value.CompareTo(b.Value) < 0 ? b : a;
    else if (a.HasValue)
        return a;
    else
        return b;
}

// Sample usage:
public DateTime? CalculateDate(DataRow row)
{
    DateTime? result = null;
    if (!(row["EXPIRATION_DATE"] is DBNull))
        result = DateTime.Parse((string)row["EXPIRATION_DATE"]);
    if (!(row["SHIPPING_DATE"] is DBNull))
        result = CoalesceMax(
            result
            DateTime.Parse((string)row["SHIPPING_DATE"]).AddYears(1));
    // etc.
    return result;
}
public static Nullable CoalesceMax(Nullable a,Nullable b),其中T:IComparable
{
if(a.HasValue&&b.HasValue)
返回a.Value.CompareTo(b.Value)<0?b:a;
else if(a.HasValue)
返回a;
其他的
返回b;
}
//示例用法:
公共约会时间?计算日期(数据行)
{
DateTime?结果=null;
如果(!(第[“到期日”]行为DBNull))
结果=DateTime.Parse((字符串)行[“到期日”]);
如果(!(第[“发货日期”]行为DBNull))
结果=聚结最大值(
结果
解析((字符串)行[“SHIPPING_DATE”]).AddYears(1));
//等等。
返回结果;
}
在编译过程中会出现以下错误:


类型“T”必须是不可为null的值类型,才能将其用作泛型类型或方法“System.nullable”中的参数“T”。您需要添加
T:struct
约束:

public static Nullable<T> CoalesceMax<T>
    (Nullable<T> a, Nullable<T> b) where T : struct, IComparable
public static Nullable CoalesceMax
(可为null的a,可为null的b)其中T:struct,i可比较
否则C#将试图找出
Nullable
的含义,并意识到它本身并不具备
Nullable
所要求的约束。换句话说,您可以尝试呼叫:

CoalesceMax<string>(...)
CoalesceMax(…)

这没有意义,因为
Nullable
无效。

您的通用方法使用的是
Nullable

但是,您没有约束
t
的类型,因此它可能最终成为
可空的
,这显然是无效的

您需要将约束更改为
,其中T:struct,IComparable
,以确保
T
只能是值类型。

可为空的
类型上有一个约束,要求
T
成为值类型(
struct
,在C#)。这就是为什么编译器告诉你关于
Nullable
而不是你的函数或该函数的调用位置的原因-
Nullable
类是错误的根本原因,因此如果编译器只是指向你的函数并说“这不对,请修复它”,这实际上会更有帮助(想象一下,如果
CoalesceMax
使用了几个泛型,并且只违反了其中一个泛型的约束,那么知道哪个泛型的约束被破坏比只知道
CoalesceMax
中的一个或多个约束被破坏更有用)

解决方案是通过引入相同的约束,使您的
T
与其
T
兼容。这是通过添加
struct
约束来实现的,该约束必须位于所有接口/新约束之前:

public static Nullable<T> CoalesceMax<T>(Nullable<T> a, Nullable<T> b) where T : struct, IComparable{
  ...
}
public static Nullable CoalesceMax(Nullable a,Nullable b),其中T:struct,IComparable{
...
}

不完全是OP的答案,但由于这是google上针对同一错误消息弹出的第一条消息,因此我不得不在我的类定义上添加约束,而不是我的方法,例如

public class MyClass<T> where T : struct
{
    public void MyMethod(T? value)
    {
    }
}
公共类MyClass,其中T:struct
{
公共方法(T?值)
{
}
}

编译器错误为您提供了函数定义的行,因为这就是错误所在。使用C#8 Not nullable标志如何?@kirinnee:不,因为
nullable
仍然需要
T
成为不可为空的值类型,而不仅仅是任何不可为空的类型。