首页 新闻 会员 周边

谁会定义可空类型的泛型方法????

0
悬赏园豆:30 [已解决问题] 解决于 2018-10-26 11:04

正常的定义方法是这样
public static bool In<T> (this T obj, T[] array)
{
return true;
}

但是这个方法如果这样调用
int? inta=1;
int [] arr={1,2};
inta.in(arr) 就会提示inta 可空不存在泛型方法in ,是因为inta 是可空的int
如果修改上面的泛型方法 支持可空类型??

我这样写 语法错误

public static bool In Nullable<T>(this Nullable<T> obj, Nullable<T>[] array)
{
return true;
}

==============
谁知道怎么定义可空泛型方法???

IDOER的主页 IDOER | 初学一级 | 园豆:19
提问于:2018-10-26 10:00
< >
分享
最佳答案
1

哈哈,试出来了。
public static class Demo
{
public static bool IfIn<T>(this T? obj, T?[] array) where T : struct
{
return true;
}
}

int? inta = 1;
int?[] arr = { 1, 2 };
inta.IfIn(arr);

坐等拿豆豆啦~~~~

收获园豆:30
Jeffcky | 老鸟四级 |园豆:2789 | 2018-10-26 10:31

nice

IDOER | 园豆:19 (初学一级) | 2018-10-26 11:04
其他回答(1)
-1

public void ThemeSet()
{
SetA<int>();
}

    public void SetA<T>(params T[] t)
    {            
    }
花飘水流兮 | 园豆:13560 (专家六级) | 2018-10-26 11:02
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册