HomeCSharpC# Error CS0832 – An expression tree may not contain an assignment operator

C# Error CS0832 – An expression tree may not contain an assignment operator

C# Error

CS0832 – An expression tree may not contain an assignment operator

Reason for the Error & Solution

An expression tree may not contain an assignment operator.

An expression tree does not preserve variable state or have any concept of a storage location.

To correct this error

  1. Remove the assignment operator from the lambda or query expression.

Example

In the example code, as in all lambda expressions, x is just an input parameter being passed by value. Its value cannot be changed in an expression tree. It can be changed in a delegate lambda.

// cs0843.cs  
using System;  
using System.Linq;  
using System.Linq.Expressions;  
  
public class C  
{  
    public static int Main()  
    {  
        Expression<Func<int, int>> e = x => x += 5; // CS0843  
        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...