I want a function that does this:
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();
}
...
}
But for all Types. I’d like to avoid writing a giant else if statement. Is there any other way to do this? I’m using C# 4.0.
For a value type, the default constructor would work.
Then you can do other stuff, like testing for a Zero method on the type and if so, calling that.