Declare Lambda Expression as a class constant field
Asked Answered
S

2

5

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

Sandpaper answered 5/10, 2013 at 12:53 Comment(7)
Are you sure we see your exact code? Do you use s in another sense elsewhere?Ullund
@JeppeStigNielsen Yes I'm sure. s is not used anywhere else. Don't you have this problem? Can you declare such a field?Sandpaper
@Alireza: Yes you can. I just copy/pasted the code and it worked just fine. EDIT: Unless, did you mean you wanted public LambdaExpression... instead of public Expression<...?Neoarsphenamine
@JeppeStigNielsen Careless me! I have problem when it is going to be declared as const. Updated the question. Thanks for your hint.Sandpaper
Based on your update, you can't use const, you'd have to use readonly. const requires that it be a compile-time constant; something you could imagine being copy/pasted wherever you use MyExpr. readonly will be resolved at runtime as though you just were referencing a field.Neoarsphenamine
@ChrisSinclair You are right. I lost the main point of the constants. Thanks so much.Sandpaper
The only const value of type Expression<Func<string, bool>> you can have, is null. But the error message was confusing. I have not tried to compile the above code myself (yet).Ullund
H
8

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.

Hassi answered 5/10, 2013 at 14:30 Comment(0)
U
4

Reproduced. That is a strange error message from the compiler. I would expect instead:

error CS0134: '(field)' is of type '(type)'. A const field of a reference type other than string can only be initialized with null.

The message we do get is misleading. Some C# expressions (I am not talking about .NET expression trees Expression<...>) can clearly contain a lambda expression, but they don't say why this particular expression cannot.

The solution is to make a static readonly field instead:

class MyClass
{
   public static readonly Expression<Func<string, bool>> MyExpr
     = s => s == "Hello!";
}

Only one instance of Expression<> will ever be created, but it is no compile-time constant, there is actually some code that will run once (just) before MyClass is used for the first time.

Ullund answered 5/10, 2013 at 14:33 Comment(2)
Some C# expressions (I am not talking about .NET expression trees Expression<...>) can clearly contain a lambda expression would you please make an example?Sandpaper
@Sandpaper The expression filtered = myList.Where(x => x.Status.IsValid) does contain a lambda expression.Ullund

© 2022 - 2024 — McMap. All rights reserved.