C# Error CS0835 – Cannot convert lambda to an expression tree whose type argument ‘{0}’ is not a delegate type

C# Error

CS0835 – Cannot convert lambda to an expression tree whose type argument ‘{0}’ is not a delegate type

Reason for the Error & Solution

Cannot convert lambda to an expression tree whose type argument ‘type’ is not a delegate type.

If a lambda expression is converted to an expression tree, the expression tree must have a delegate type for its argument. Furthermore, the lambda expression must be convertible to the delegate type.

To correct this error

  1. Change the type parameter from int to a delegate type, for example Func<int,int>.

Example

The following example generates CS0835:

// cs0835.cs  
using System;  
using System.Linq;  
using System.Linq.Expressions;  
  
public class C  
{  
    public static int Main()  
    {  
        Expression<int> e = x => x + 1; // CS0835  
  
        // Try the following line instead.  
       // Expression<Func<int,int>> e2 = x => x + 1;  
  
        return 1;  
    }  
}  

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