Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Default parameter value for must be a compile-time constant
Example
The following sample generates CS1736:
// CS1736.cs
public unsafe class C
{
static void F(int i = G())
{
// ...
}
static int G() => 0;
A default parameter value is evaluated upon the invocation of the method. What a value may be when the method is eventually invoked cannot be pre-determined at declaration-time unless that value is constant at compile-time.
To correct this error
If a dynamically evaluated value is required, consider using a compile-time constant as a marker value which is then checked at run-time:
static void F(int i = -1)
{
if(i == -1) i = G();
//...
}