HomeCSharpC# Error CS0834 – A lambda expression with a statement body cannot be converted to an expression tree

C# Error CS0834 – A lambda expression with a statement body cannot be converted to an expression tree

C# Error

CS0834 – A lambda expression with a statement body cannot be converted to an expression tree

Reason for the Error & Solution

A lambda expression must have an expression body to be converted to an expression tree.

Lambdas that are translated to expression trees must be expression lambdas; statement lambdas and anonymous methods can only be converted to delegate types.

To correct this error

  1. Remove the statement from the lambda expression.

Example

The following example generates CS0834:

// cs0834.cs  
using System;  
using System.Linq;  
using System.Linq.Expressions;  
  
public class C  
{  
    public static int Main()  
    {  
        Expression<Func<int, int>> e = x => { return x; }; // CS0834  
    }  
}  

Leave a Reply

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