类型'T'必须是为了用它作为参数“T”中的泛型类型或方法非空值类型'System.Nullable< T>' [英] 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>'
问题描述
为什么会出现这个错误在下面的代码?
无效的主要()
{
INT?一个= 1;
诠释? B = AddOne(1);
a.Dump();
}
静态可空< INT> AddOne(可空< INT>可为空)
{
返回ApplyFunction< INT,INT>(可空,(INT X)=> X + 1);
}
静态可空< T> ApplyFunction< T,TResult>(可空< T>空的,Func键< T,TResult>功能)
{
如果(nullable.HasValue)
{$ B $(B T)展开=空。值;
TResult结果=功能(展开);
返回新的可空< TResult>(结果);
}
,否则
{
返回新的可空< T>();
}
}
有几个问题的代码。第一个是你的类型必须为空。您可以通过指定的表达其中T:结构
。您还需要指定,其中TResult:结构
,因为你使用的是可空类型太
一旦你确定了其中T:结构,其中TResult:结构
你也需要改变返回值的类型(这是错误的)和一些其他的东西太多
修正所有错误和简化后,风与这样的:
静态TResult? ApplyFunction< T,TResult>(T为空的,Func键< T,TResult>功能?)
,其中T:结构
其中TResult:结构
{
如果(nullable.HasValue)
复位功能(nullable.Value);
,否则
返回NULL;
}
请注意,您可以重写可空< T>
为 T?
这使得事情更易读。
你也可以写为一体using语句:
,但我不认为它的可读性:
返回nullable.HasValue? (TResult?)功能(nullable.Value):空;
您可能希望把这个变成一个扩展方法
公共静态类NullableExt
{
公共静态TResult? ApplyFunction< T,TResult>(这件T为空的,Func键< T,TResult>功能?)
,其中T:结构
其中TResult:结构
{
如果(nullable.HasValue )
返回功能(nullable.Value);
,否则
返回NULL;
}
}
然后,你可以这样写代码:
诠释? X = 10;
翻番? X1 = x.ApplyFunction(ⅰ= GT;的Math.sqrt(ⅰ));
Console.WriteLine(×1);
诠释? Y = NULL;
翻番? Y1 = y.ApplyFunction(ⅰ= GT;的Math.sqrt(ⅰ));
Console.WriteLine(Y1);
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>();
}
}
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: struct
because you're using that as a nullable type too.
Once you fix up the where T: struct where TResult: struct
you also need to change the return value type (which was wrong) and a number of other things too.
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.
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);
这篇关于类型'T'必须是为了用它作为参数“T”中的泛型类型或方法非空值类型'System.Nullable< T>'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!