I need to have a class that has a constructor with BigInteger type. I’d like to set a default value to 0 to it with this code, but I got compile error.
public class Hello
{
BigInteger X {get; set;}
public Hello(BigInteger x = 0)
{
X = x;
}
}
public class MyClass
{
public static void RunSnippet()
{
var h = new Hello(); // Error <--
What’s wrong? Is there way to set default value to BigInteger parameter?
Default parameters only work compile time constants (and value types where the parameter can be
default(ValType)ornew ValType()), and as such don’t work for BigInteger. For your case, you could perhaps do something like this; providing a constructor overload that takes int, and make that have a default parameter:In case you are wondering, the
x = 0doesn’t count as a constant when the type is aBigInteger, because converting 0 toBigIntegerwould involve invoking the implicit conversion operator for int to BigInteger.