C# Error CS1065 – Default values are not valid in this context.

C# Error

CS1065 – Default values are not valid in this context.

Reason for the Error & Solution

Default values are not valid in this context.

Default values are used when declaring optional arguments. Optional arguments are not supported when declaring an anonymous method with the delegate operator. The delegate operator creates an anonymous method.

Example

The following sample generates CS1065:

// CS1065.cs (5,27)

class A
{
    delegate void D(int x);
    D d1 = delegate(int x = 42) { };
}

To correct this error

Removing the default value corrects this error:

class A
{
    delegate void D(int x);
    D d1 = delegate(int x) { };
}

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