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

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