Resolve errors and warnings generated from expressions prohibited in expression trees

This article covers the following compiler errors:

  • CS0765 - Partial methods with only a defining declaration or removed conditional methods cannot be used in expression trees.
  • CS0831 - An expression tree may not contain a base access.
  • CS0832 - An expression tree may not contain an assignment operator
  • CS0834 - A lambda expression with a statement body cannot be converted to an expression tree.
  • CS0835 - Cannot convert lambda to an expression tree whose type argument 'type' is not a delegate type.
  • CS0838 - An expression tree may not contain a multidimensional array initializer.
  • CS0845 - An expression tree lambda may not contain a coalescing operator with a null or default literal left-hand side.
  • CS0853 - An expression tree may not contain a named argument specification.
  • CS0854 - An expression tree may not contain a call or invocation that uses optional arguments.
  • CS0855 - An expression tree may not contain an indexed property.
  • CS1944 - An expression tree may not contain an unsafe pointer operation.
  • CS1945 - An expression tree may not contain an anonymous method expression.
  • CS1946 - An anonymous method expression cannot be converted to an expression tree.
  • CS1951 - An expression tree lambda may not contain a ref, in or out parameter.
  • CS1952 - An expression tree lambda may not contain a method with variable arguments.
  • CS1963 - An expression tree may not contain a dynamic operation.
  • CS1989 - Async lambda expressions cannot be converted to expression trees.
  • CS2037 - An expression tree lambda may not contain a COM call with ref omitted on arguments.
  • CS7053 - An expression tree may not contain "feature".
  • CS8072 - An expression tree lambda may not contain a null propagating operator.
  • CS8074 - An expression tree lambda may not contain a dictionary initializer.
  • CS8075 - An extension Add method is not supported for a collection initializer in an expression lambda.
  • CS8110 - An expression tree may not contain a reference to a local function.
  • CS8122 - An expression tree may not contain an 'is' pattern-matching operator.
  • CS8143 - An expression tree may not contain a tuple literal.
  • CS8144 - An expression tree may not contain a tuple conversion.
  • CS8153 - An expression tree lambda may not contain a call to a method, property, or indexer that returns by reference.
  • CS8155 - Lambda expressions that return by reference cannot be converted to expression trees.
  • CS8188 - An expression tree may not contain a throw-expression.
  • CS8198 - An expression tree may not contain an out argument variable declaration.
  • CS8207 - An expression tree may not contain a discard.
  • CS8382 - An expression tree may not contain a tuple == or != operator.
  • CS8514 - An expression tree may not contain a switch expression.
  • CS8640 - Expression tree cannot contain value of ref struct or restricted type.
  • CS8642 - An expression tree may not contain a null coalescing assignment.
  • CS8790 - An expression tree may not contain a pattern System.Index or System.Range indexer access.
  • CS8791 - An expression tree may not contain a from-end index ('^') expression.
  • CS8792 - An expression tree may not contain a range ('..') expression.
  • CS8810 - '&' on method groups cannot be used in expression trees.
  • CS8849 - An expression tree may not contain a with-expression.
  • CS8927 - An expression tree may not contain an access of static virtual or abstract interface member.
  • CS8952 - An expression tree may not contain an interpolated string handler conversion.
  • CS8972 - A lambda expression with attributes cannot be converted to an expression tree.
  • CS9170 - An expression tree may not contain an inline array access or conversion.
  • CS9175 - An expression tree may not contain a collection expression.

Expression tree restrictions

All of the errors in the preceding list indicate you've used a C# expression type that isn't allowed in an expression tree. In most cases, the prohibited expressions represent syntax introduced after C# 3.0. These expressions are prohibited because allowing them would create a breaking change in all libraries that parse expression trees. All libraries would need to be enhanced to parse new C# expressions if newer constructs were allowed.

The following expressions are prohibited:

Other restrictions are:

  • Attributes can't be applied to the lambda expression, its parameters or return.
  • The lambda expression must be convertible to a type derived from System.Linq.Expressions.Expression whose type parameter is a delegate type.
  • named and optional parameters are restricted. The expression can't call a method specifying named arguments, and it can't use the default value of an optional parameter.
  • Dictionary initializers aren't allowed. Neither are extension Add methods.
  • The target expression must be a lambda expression. Constants and variables aren't allowed, but a lambda expression that returns a constant or variable is.
  • Unsafe pointer operations aren't allowed.
  • COM calls must include ref on arguments; it can't be implied.
  • The unsupported __arglist keyword isn't allowed.