What's the purpose of the Expression class?

Steffen picture Steffen · Oct 14, 2011 · Viewed 13.9k times · Source

I'm wondering what exactly is the difference between wrapping a delegate inside Expression<> and not ?

I'm seeing Expression<Foo> being used a lot with LinQ, but so far I've not found any article that explains the difference between this, and just using a delegate.

E.g.

Func<int, bool> Is42 = (value) => value == 42;

vs.

Expression<Func<int, bool>> Is42 = (value) => value == 42;

Answer

Jeff Mercado picture Jeff Mercado · Oct 14, 2011

By storing a lambda as a delegate, you are storing a specific instance of a delegate that does some action. It can't be modified, you just call it. Once you have your delegate, you have limited options in inspecting what it does and whatnot.

By storing a lambda as an expression, you are storing an expression tree that represents the delegate. It can be manipulated to do other things like changing its parameters, changing the body and make it do something radically different. It could even be compiled back to a delegate so you may call it if you wish. You can easily inspect the expression to see what its parameters are, what it does and how it does it. This is something that a query provider can use to understand and translate an expression to another language (such as write an SQL query for a corresponding expression tree).

It is also a whole lot easier to to create a delegate dynamically using expressions than it is emitting the code. You can think of your code at a higher level as expressions that is very similar to how a compiler views code instead of going low level and view your code as IL instructions.

So with an expression, you are capable to do much more than a simple anonymous delegate. Though it's not really free, performance will take a hit if you run compiled expressions compared to a regular method or an anonymous delegate. But that might not be an issue as the other benefits to using expressions may be important to you.