I understand lambdas and the Func
and Action
delegates. But expressions stump me. In what circumstances would you use an Expression<Func<T>>
rather than a plain old Func<T>
?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
You would use an expression when you want to treat your function as data and not as code. You can do this if you want to manipulate the code (as data). Most of the time if you don't see a need for expressions then you probably don't need to use one.
The primary reason is when you don't want to run the code directly, but rather, want to inspect it. This can be for any number of reasons:
I'd like to add some notes about the differences between
Func<T>
andExpression<Func<T>>
:Func<T>
is just a normal old-school MulticastDelegate;Expression<Func<T>>
is a representation of lambda expression in form of expression tree;Func<T>
;ExpressionVisitor
;Func<T>
;Expression<Func<T>>
.There's an article which describes the details with code samples:
LINQ: Func<T> vs. Expression<Func<T>>.
Hope it will be helpful.
LINQ is the canonical example (for example, talking to a database), but in truth, any time you care more about expressing what to do, rather than actually doing it. For example, I use this approach in the RPC stack of protobuf-net (to avoid code-generation etc) - so you call a method with:
This deconstructs the expression tree to resolve
SomeMethod
(and the value of each argument), performs the RPC call, updates anyref
/out
args, and returns the result from the remote call. This is only possible via the expression tree. I cover this more here.Another example is when you are building the expression trees manually for the purpose of compiling to a lambda, as done by the generic operators code.
There is a more philosophical explanation about it from Krzysztof Cwalina's book(Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries);
Edit for non-image version:
When you want to treat lambda expressions as expression trees and look inside them instead of executing them. For example, LINQ to SQL gets the expression and converts it to the equivalent SQL statement and submits it to server (rather than executing the lambda).
Conceptually,
Expression<Func<T>>
is completely different fromFunc<T>
.Func<T>
denotes adelegate
which is pretty much a pointer to a method andExpression<Func<T>>
denotes a tree data structure for a lambda expression. This tree structure describes what a lambda expression does rather than doing the actual thing. It basically holds data about the composition of expressions, variables, method calls, ... (for example it holds information such as this lambda is some constant + some parameter). You can use this description to convert it to an actual method (withExpression.Compile
) or do other stuff (like the LINQ to SQL example) with it. The act of treating lambdas as anonymous methods and expression trees is purely a compile time thing.will effectively compile to an IL method that gets nothing and returns 10.
will be converted to a data structure that describes an expression that gets no parameters and returns the value 10:
larger image
While they both look the same at compile time, what the compiler generates is totally different.