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

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