C# Error CS1763 – ‘{0}’ is of type ‘{1}’. A default parameter value of a reference type other than string can only be initialized with null

C# Error

CS1763 – ‘{0}’ is of type ‘{1}’. A default parameter value of a reference type other than string can only be initialized with null

Reason for the Error & Solution

A default parameter value of a reference type other than string can only be initialized with null

Example

The following sample generates CS1763:

// CS1763.cs (0,0)
class Program
{
    public void Goo<T, U>(T t = default(U)) where U : T
    {
    }
    static void Main(string[] args)
    {
        
    }
}

This example generates CS1763 because the Goo<T,U> parameter is declared with a default value of default(U) when the type of the parameter is T, despite the constraint that U derive from base class T.

To correct this error

Changing default(U) to use the corresponding type argument corrects this error:

    public void Goo<T, U>(T t = default(T)) where U : T
    {
    }

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