Demystifying C# 3.0 - Part 7: Expression Trees

Posted on 6/30/2006 @ 8:47 PM in #Vanilla .NET by | Feedback | 9324 views

In this series of Demystifying C# 3.0 we have already covered -

a) Demystifying C# 3.0 - Part 1: Implicitly Typed Local Variables "var"
b) Demystifying C# 3.0 - Part 2: Anonymous Types
c) Demystifying C# 3.0 - Part 3: Extension Methods
d) Demystifying C# 3.0 - Part 4: Lambda Expressions
e) Demystifying C# 3.0 - Part 5: Object and Collection Initializers
f)  Demystifying C# 3.0 - Part 6: (LINQ) Query Expression Translation (to C# 3.0)

Thank you to both Eric Wise, and C. Steen for linking. The more people we have look at these posts, the better the overall discussion quality will be.

Okay, so next we will be talking about "Expression Trees". You must read about Lambda Expressions before you read this post.

So in short, Expression trees permit lambda expressions to be represented as data structures, instead of executable code. What that means is, a lambda expression such as

x => x + 1, is executable code. which could also be written as,

Func<int,int> f = x => x + 1

But,

Expression<Func<int,int>> e = x => x + 1 ;

In other words, if a lambda expression can convert to a Delegate type "D", it can also convert to an expression tree of type System.Query.Expression<D>.

".. if a lambda expression can convert to a Delegate type "D", it can also convert to an expression tree of type System.Query.Expression<D>.. " <-- That fills out that little blurb I left out when discussing Lambda Expressions.

This covers the basics of C# 3.0. Next we will be continuing this talk, with a continued discussion on LINQ.

Sound off but keep it civil:

Older comments..


On 3/9/2009 9:17:26 AM Andy said ..
Really? You honestly think that this "demystifies" Expression Trees?


On 6/5/2009 11:19:44 AM Erik said ..
What the other guy said...


On 8/13/2009 11:21:36 AM decius said ..
Agreed. This is quite dry and falls short of demystificationism


On 11/10/2009 6:45:56 AM ex-lisper said ..
Wow, short and sweet... which is a shame, as your other articles were fantastic.


On 11/10/2009 3:27:17 PM Sahil Malik said ..
Yeah I need to write this article better. I'll get around to it one day!


On 12/17/2009 5:19:53 AM anon said ..
u lazy bum :) it's been 3 years already. write it better now!


On 9/11/2010 7:05:00 PM anon2 said ..
The following describes Expression Trees very well:


http://blogs.msdn.com/b/charlie/archive/2008/01/31/expression-tree-basics.aspx