类型'T'必须是非可空值类型,以便在泛型类型或方法'System.Nullable <T>'中将其用作参数'T'

Cli*_*ive 13 c# generics nullable

为什么我在以下代码中收到此错误?

void Main()
{
    int? a = 1;
    int? b = AddOne(1);
    a.Dump();
}

static Nullable<int> AddOne(Nullable<int> nullable)
{
    return ApplyFunction<int, int>(nullable, (int x) => x + 1);
}

static Nullable<T> ApplyFunction<T, TResult>(Nullable<T> nullable, Func<T, TResult> function)
{
    if (nullable.HasValue)
    {
        T unwrapped = nullable.Value;
        TResult result = function(unwrapped);
        return new Nullable<TResult>(result);
    }
    else
    {
        return new Nullable<T>();
    }
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*son 16

代码有几个问题.第一个是你的类型必须是可空的.您可以通过指定来表达where T: struct.您还需要指定,where TResult: struct因为您也将其用作可空类型.

一旦你修好了,where T: struct where TResult: struct你还需要改变返回值类型(这是错误的)和其他一些东西.

在解决了所有这些错误并简化之后,你最终得到了这样的结论:

static TResult? ApplyFunction<T, TResult>(T? nullable, Func<T, TResult> function)
                where T: struct 
                where TResult: struct
{
    if (nullable.HasValue)
        return function(nullable.Value);
    else
        return null;
}
Run Code Online (Sandbox Code Playgroud)

请注意,您可以重写Nullable<T>T?这使得事情更易读.

你也可以把它写成一个语句,?:但我不认为它是可读的:

return nullable.HasValue ? (TResult?) function(nullable.Value) : null;
Run Code Online (Sandbox Code Playgroud)

您可能希望将其放入扩展方法中:

public static class NullableExt
{
    public static TResult? ApplyFunction<T, TResult>(this T? nullable, Func<T, TResult> function)
        where T: struct
        where TResult: struct
    {
        if (nullable.HasValue)
            return function(nullable.Value);
        else
            return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以写这样的代码:

int? x = 10;
double? x1 = x.ApplyFunction(i => Math.Sqrt(i));
Console.WriteLine(x1);

int? y = null;
double? y1 = y.ApplyFunction(i => Math.Sqrt(i));
Console.WriteLine(y1);
Run Code Online (Sandbox Code Playgroud)

  • 而不是写"新的TResult?()",写"null"不是更清楚吗?我想这就是"Nullable <>"的哲学. (2认同)

T. *_*ley 6

正如错误所示,编译器无法保证T不会是可空的.您需要向T添加约束:

static Nullable<T> ApplyFunction<T, TResult>(Nullable<T> nullable, 
    Func<T, TResult> function) : where T : struct 
                                 where TResult : struct
Run Code Online (Sandbox Code Playgroud)