What are the benefits of expression trees in a table?
Expression trees allow a program to manage part of its own implementation at run time in a simplified way. They allow you to pass expressions as a representation of their structure, and not just as a delegate that you can call.
What exactly does this operator mean: "Expression trees are data compiled as code"?
Languages ββbefore C # /. NET supported this kind of manipulation ... the best example is LISP. The ability to represent the structure of a program in a data structure within a program is called homoiconicity . C # supports limited homoconicity in the form of expression trees. C # allows you to create expression trees transparently from (a subset) of expressions in your code. For instance:
int x = 3; Expression<Func<bool>> IsXLessThan4Expr = () => x < 4; Func<bool> IsXLessThan4 = () => x < 4;
The variable IsXLessThan4Expr is captured from the lambda expression as an expression tree. Now we can cross the representation of this expression if we like to understand what kind of structure it is - and manipulate it if we want. The IsXLessThan4 delegate, by contrast, cannot be verified ... it can only be called. Of course, you can always get the original IL for any method (provided that you have the necessary permissions), but it is much more difficult to change the logical structure of the program from IL than from the expression tree.
What common problems do expression trees and functional programming cause in a .net context?
The best example of using expression trees to solve a non-trivial problem is in LINQ-to-SQL, where the IQueryable implementation IQueryable able to convert query expression trees written in C # into equivalent SQL queries that can be executed using a database.
Expression trees also allow you to generate C # code on the fly - since expression trees can be compiled into lambdas. Here is an example of this:
var paramNotification = Expression.Parameter(typeof (NotificationEntry), "noti"); Func<NotificationEntry, bool> predicate = m_PredicateExpr = Expression.Lambda<Func<NotificationEntry, bool>>( Expression.LessThan( Expression.Property(paramNotification, "Value"), Expression.Constant(100)), new[] {paramNotification}) .Compile();
The above snippet creates an expression that compares the Value field of the NotificationEntry object with some provided constant (100) - and compiles it into a lambda that we can call.
What are some good online resources to get speed on this?
MSDN is probably the best choice at the moment.