I was reading about Expression Tree feature and how you can create delegates using lambda expressions. I still can’t get as to in what scenario it is useful and in what real world example should I use it.
I was reading about Expression Tree feature and how you can create delegates using
Share
The primary use for expression trees is for out-of-process LINQ providers such as LINQ to SQL.
When you write something like this:
those lambda expressions can either be converted to delegates, which can then be executed (as they are in LINQ to Object) or they can be converted to expression trees, which can be analyzed by the query source and acted on accordingly (e.g. by turning them into SQL, web service calls etc). The difference is that expression trees represent the code as data. They can be compiled into delegates if necessary, but usually (within LINQ anyway) they’re never executed directly – just examined to find out the logic they contain.
Expression trees are also used extensively in the Dynamic Language Runtime, where they represent the code which should execute when a dynamic expression is evaluated. Expression trees are well suited for this as they can be composed and broken down again, and after they’re compiled the resulting IL is JIT-compiled as normal.
Most developers will never need to mess with the expression tree API, although it has a few other uses.