HomeCSharpC# Error CS1737 – Optional parameters must appear after all required parameters

C# Error CS1737 – Optional parameters must appear after all required parameters

C# Error

CS1737 – Optional parameters must appear after all required parameters

Reason for the Error & Solution

Optional parameters must appear after all required parameters

The compiler does not support optional parameters being declared before required parameters. All optional parameters must be after all required parameters.

Example

The following sample generates CS1737:

// CS1737.cs (7,45)
class C
{
    static void F(object? x)
    {
        G(y: x);
    }
    static void G(object? x = null, object y)
    {
    }
}

To correct this error

The signature for this method may be changed without effecting existing code that calls the method because a value for the optional parameter has not been used. For example:

// CS1737.cs (7,45)
class C
{
    static void F(object? x)
    {
        G(y: x);
    }
    static void G(object y, object? x = null)
    {
    }
}

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...