using System;
using System.IO;
class Program
{
static bool IsNullable<T>(T t)
{
return false;
}
static bool IsNullable<T>(T? t) where T : struct
{
return true;
}
static void Test(params object[] objs)
{
for (int iter = 0, limit = objs.Length; iter < limit; ++iter)
{
var obj = objs[iter];
Console.WriteLine("{0}: {1}", iter, IsNullable(obj) ? "yes" : "no");
}
}
static void Main(string[] args)
{
Test(new int?(100));
}
}
我传递一些数字作为nullables
的功能,因此它们与null
取代,如果0
添加到数据库。我认为这是一种简单而有意义的方式,不需要创建新的数据类型。但是...它不起作用。检查中params对象[]对象为空<T>
所以我试图测试objs
为Nullable
类型。 这可以做到吗?如果进入params object[]
,则Nullable<T>
转换为T
。
我肯定会使用自定义的数据类型,但为什么这不工作,因为我认为它会?
PS:我是非常新的C#
。
更好的问题是为什么你使用对象数组而不是更具体的东西? – Stilgar 2014-09-27 19:06:54
试过[Nullable.GetunderlyingType()](http://msdn.microsoft.com/de-de/library/system.nullable.getunderlyingtype(v = vs.110).aspx)? – user3411327 2014-09-27 19:07:13
@ user3411327是的。不工作。 – CodeAngry 2014-09-27 19:09:03