I have this expression :
Expression<Func<string, bool>> f = s => s.Length < 5;
ParameterExpression p = Expression.Parameter (typeof (string), "s");
MemberExpression stringLength = Expression.Property (p, "Length");
ConstantExpression five = Expression.Constant (5);
BinaryExpression comparison = Expression.LessThan (stringLength, five);
Expression<Func<string, bool>> lambda= Expression.Lambda<Func<string, bool>> (comparison, p);
//lets : test
Func<string, bool> runnable = lambda.Compile();
Console.WriteLine (runnable ("kangaroo")); // False
Console.WriteLine (runnable ("dog")); //True
I want to ask about the .Compile()
What does it compile ? And what is the difference between the first execution vs later executions...?
Compile should be something that happens once and not happens again later ....
What / How does it help me ?
The
Expression<Func<string,bool>>
is only a representation of an expression, it cannot be executed. CallingCompile()
gives you a compiled delegate, a piece of code that you can call. Essentially, your program composes a small code snippet at runtime, and then call it as if it were processed by the compiler. This is what the last two lines of your code do: as you can see, the compiled snippet can analyze the length of the string that you pass in - when the length is less than five, you get aTrue
back; when it's five or more, you get aFalse
.What happens on first execution of the compiled snippet is platform-dependent, and should not be detectable by programmers using the .NET platform.
Compile()
takes the expression tree (which is a data representation of some logic) and converts it to IL which can then be executed directly as a delegate.The only difference between the first execution and later executions is the possibility that
Compile()
won't trigger the JIT compilation from IL to native processor code. That may happen on first execution.When you are building the expression tree at runtime there's no code emitted. It's a way to represent .NET code at runtime.
Once you call the
.Compile
method on the expression tree the actual IL code is emitted to convert this expression tree into a delegate (Func<string, bool>
in your case) that you could invoke at runtime. So the code that this expression tree represents can be executed only after you compile it.Calling Compile is an expensive operation. Basically you should be calling it once and then caching the resulting delegate that you could use to invoke the code many times.