Wednesday, January 30, 2008

C#'s Syntatic Sugar gets a Little Spicy

So far I've bought into the "All of C# 3.0's new language features are just syntatic sugar" view. Today I realized that this sugar has some kick to it.

If Lambda Functions are syntatic sugar, then Expression Trees are habanero peppers.

A lambda function is a new way to declare an anonymous delegate, which is effectively a function pointer. Lambda functions are compiled into executable code.

Expression Trees are data structures that contain descriptions of lambda functions. They convert your code into data. Somewhere down the road in your application a block of code will call Compile() on the expression tree to get a reference to the lambda function and then call Invoke() to execute the lambda.

I found these articles helped me understand Expression Trees:

Expression trees are used heavily by Linq and Linq to Sql. It's important to understand Expression Trees if you want to develop your own Linq IQueryProvider, which is not a trivial task!

2 comments:

  1. You might want to check out http://blogs.msdn.com/charlie/archive/2008/01/31/expression-tree-basics.aspx for a great discussion of expression trees

    ReplyDelete
  2. Not that I'm totally impressed, but this is more than I expected for when I found a link on SU telling that the info here is awesome. Thanks.

    ReplyDelete