You almost found the answer already.
i => i + 1
is not a Func<int, int>
. It is a lambda expression. Lambda expressions can be converted to a matching delegate type, or a matching expression tree type.
If a lambda expression is converted to a delegate type, the compiler compiles this into IL code that has the specified effect.
If a lambda expression is converted to an expression tree type, the compiler compiles this into IL that generates an expression tree that represents what you wrote in your lambda expression. Expression trees are meant for later parsing by libraries, so it's important for the expression tree to closely match the code you wrote.
Func<int, int> f = i => i + 1; // okay, creates delegate.
Expression<Func<int, int>> e = i => i + 1; // okay, creates expression tree.
No information can be retrieved from f
about the operation it performs. It is known from its delegate type that it takes an int
and returns an int
, but other than that, it is a black box. You put a number in, and you get a number out, but you no longer know how.
e
, on the other hand, stores the fact that 1
is added to a parameter named i
, and even that 1
appears on the RHS of the +
.
This extra information that is stored in e
is often essential to those libraries that interpret expression trees, so an implicit conversion from Func<int, int>
to Expression<Func<int, int>>
just wouldn't work: the required information is no longer available.
As for LambdaExpression decrementorExpression = (i => i - 1);
, that is invalid simply because there is no way for the compiler to determine whether you want an Expression<Func<int, int>>
, an Expression<Func<int, object>>
, or an Expression<MyFunc>
where MyFunc
is a custom delegate type you created.
And finally "An expression tree may not contain an assignment operator", that's mainly because for the intended use cases of expression trees, it is generally not meaningful for the expression to contain an assignment. It is a somewhat arbitrary restriction, though, considering that .NET expression trees are capable of representing assignment operations.