C# Error CS1662 – Cannot convert {0} to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type

C# Error

CS1662 – Cannot convert {0} to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type

Reason for the Error & Solution

Cannot convert anonymous method block to delegate type ‘delegate type’ because some of the return types in the block are not implicitly convertible to the delegate return type

This error occurs if the anonymous method block’s return statement had a type that was not implicitly convertible to the return type of the delegate.

The following sample generates CS1662:

// CS1662.cs

delegate int MyDelegate(int i);

class C
{

  public static void Main()
  {
     MyDelegate d = delegate(int i) { return 1.0; };  // CS1662
     // Try this instead:
     // MyDelegate d = delegate(int i) { return (int)1.0; };
  }
}

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