Search code examples
.netlinqlambdaexpression-trees

What exactly is the point of Expression Trees?


Ok, I just don't get it.

I've read about as much as I can on the subject without knowing what it's all about:

  • Why use Expression Trees?
  • What's a real-world example of when and how I'd use them?
  • What overall benefit(s) are there in using them?

Solution

  • Expression trees API originally was written to enable creation of custom LINQ providers. Basically, if you use LINQ to Objects you don't deal with Expression Trees. But if you work with LINQ to SQL then the LINQ provider works with Expression Trees.

    In .Net 4.0 Expression Trees API was extended and is heavily used by DLR. So, if you are going to let's say add a new dynamic langauge to the .NET, you'll need it.

    As it often happens, people found more ways to use expression trees. Sometimes you can use ET to get more information about objects instead of reflection. In .NET 4 you can also use them to generate dynamic methods. But remember that these are more like advanced tricks rather than recommended scenarios.

    Here are some links you can take a look at:

    Expression Tree Basics

    Getting Information About Objects, Types, and Members with Expression Trees

    Generating Dynamic Methods with Expression Trees in Visual Studio 2010