C# 类型“T”必须是不可为空的值类型,以便将其用作泛型类型或方法“System.Nullable<T>”中的参数“T”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16730753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The type 'T' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>'
提问by Clive
Why am I getting this error in the following code?
为什么我在以下代码中收到此错误?
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>();
}
}
采纳答案by Matthew Watson
There are several problems with the code. The first one is that your types must be nullable. You can express that by specifying where T: struct. You will also need to specify where TResult: structbecause you're using that as a nullable type too.
代码有几个问题。第一个是您的类型必须可以为空。您可以通过指定来表达这一点where T: struct。您还需要指定,where TResult: struct因为您也将其用作可空类型。
Once you fix up the where T: struct where TResult: structyou also need to change the return value type (which was wrong) and a number of other things too.
修复之后,where T: struct where TResult: struct您还需要更改返回值类型(这是错误的)和其他一些事情。
After fixing all those errors and simplifying, you wind up with this:
修复所有这些错误并进行简化后,您会得到以下结果:
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;
}
Note that you can rewrite Nullable<T>as T?which makes things more readable.
请注意,您可以重写Nullable<T>asT?这使事情更具可读性。
Also you could write that as one statement using ?:but I don't think it's as readable:
你也可以把它写成一个语句,?:但我认为它不那么可读:
return nullable.HasValue ? (TResult?) function(nullable.Value) : null;
You might want to put this into an extension method:
您可能希望将其放入扩展方法中:
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;
}
}
Then you can write code like this:
然后你可以写这样的代码:
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);
回答by T. Kiley
As the error suggests, the compiler has no guarantee that T won't already be nullable. You need to add a constraint to T:
正如错误所暗示的那样,编译器不能保证 T 不会已经可以为空。您需要向 T 添加约束:
static Nullable<T> ApplyFunction<T, TResult>(Nullable<T> nullable,
Func<T, TResult> function) : where T : struct
where TResult : struct

