Search code examples
linqexpression-trees

Assignment in .NET 3.5 expression trees


Is it possible to encode an assignment into an expression tree?


Solution

  • No, I don't believe so.

    Certainly the C# compiler disallows it when converting a lambda expression:

    int x;
    Expression<Func<int,int>> foo = (x=y); // Assign to x and return value
    

    This yields the error:

    CS0832: An expression tree may not contain an assignment operator