Why isn't it possible to declare a class constant filed of type Lambda Expression
. I want Something like this:
class MyClass
{
public const Expression<Func<string,bool>> MyExpr = (string s) => s=="Hello!";
}
But I get the compile error: Expression cannot contain anonymous methods or lambda expressions
This is just a limitation of C# and CLR. Only primitive numeric values, string literals and null
can be used as a value of a constant field. Expression trees are represented as a normal graph of objects and can't appear as a constant value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With