我想要一个执行此操作的函数:
private static dynamic Zero(Type T)
{
if (T == typeof(Decimal))
{
return Decimal.Zero;
}
else if (T == typeof(Double))
{
return new Double();
}
else if (T == typeof(Int64))
{
return new Int64();
}
...
}
但对于所有类型。我想避免写一个巨大的 else if 语句。还有其他方法吗?我正在使用 C# 4.0。
最佳答案
return default(T);
关于C# 在相应的输入类型中返回零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2090455/