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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...