I have a class and function
class A
{
A(int i = 0)
{
}
}
void f(A a = new A(10)) // ? default parameter value must be compiler-time constanct
{
}
How to workaround it?
I have a class and function
class A
{
A(int i = 0)
{
}
}
void f(A a = new A(10)) // ? default parameter value must be compiler-time constanct
{
}
How to workaround it?
You would need to do it inside the method and provide a comment that the method accepts null and uses A(10) as a default value.
void f(A a = null)
{
if(a == null)
a = new A(10);
}
null. You could use a = a ?? new A(10); but there is no need.null it will do the assignment still. It is an operation that doesn't need to happen. By doing the null check the assignment will only happen if a null is passed; or in this case by default.