C# Error CS1736 – Default parameter value for ‘{0}’ must be a compile-time constant

C# Error

CS1736 – Default parameter value for ‘{0}’ must be a compile-time constant

Reason for the Error & Solution

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();
        //...
    }

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...