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

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