类型'T'必须是为了用它作为参数“T”中的泛型类型或方法非空值类型'System.Nullable< T>'

编程入门 行业动态 更新时间:2024-10-17 13:37:51
本文介绍了类型'T'必须是为了用它作为参数“T”中的泛型类型或方法非空值类型'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&LT; T&GT;&

本文发布于:2023-11-02 14:14:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552627.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   用它   是为了   参数   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!