What are Expression Trees, how do you use them, and why would you use them?

37,801

An Expression Tree is a data structure that contains Expressions, which is basically code. So it is a tree structure that represents a calculation you may make in code. These pieces of code can then be executed by "running" the expression tree over a set of data.

A great thing about expression trees is that you can build them up in code; that is, you build executable code (or a sequence of steps) in code. You can also modify the code before you execute it by replacing expressions by other expressions.

An Expression is then a function delegate, such as (int x => return x * x).

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

Share:
37,801
Donald N. Mafa
Author by

Donald N. Mafa

Highly self-motivated, experienced Software Architect/ Full Stack Developer, with impeccable work ethic & a calm disposition Hard working, team player & geared to work under pressure. Over 15 years’ experience in the technology realm

Updated on July 01, 2022

Comments

  • Donald N. Mafa
    Donald N. Mafa almost 2 years

    I just came across the concept of expression trees which I have heard multiple times. I just want to understand what is meant by an expression tree and its purpose.

    I would love it if someone could also direct me to simple explanations and samples of use.

  • david.pfx
    david.pfx almost 8 years
    This isn't really true. As pointed out in the linked article, expression trees are mainly used to represent calculations that will be sent across the wire for execution elsewhere. For that reason they do not contain code; rather they are instead of code.
  • Roy Dictus
    Roy Dictus almost 8 years
    Expression Trees can be used for any calculation that you want to perform, locally or remotely. You could easily write a parser that would convert a typed expression into an Expression Tree. Also I never said that Expression Trees contain code.
  • david.pfx
    david.pfx almost 8 years
    if "tree structure with pieces of code in it" is not intended to mean they "contain code" then you should edit your answer to make that clear. You should make it clear that Expressions do not contain any IL code whatsoever, and rely on being translated further into something executable.
  • Roy Dictus
    Roy Dictus almost 8 years
    @david.pfx I would expect readers with the intelligence of software developers to understand that from my answer.
  • Gusdor
    Gusdor almost 8 years
    @RoyDictus It is not obvious without reading between the lines. Assuming intelligence is often a bad idea. Please be explicit.
  • Steve Byrne
    Steve Byrne over 7 years
    I would note also, someone who is looking for an answer to this question most likely has no knowledge of the answer and there for the "intelligence" or knowledge of the user should be assumed as 0 (on the subject at hand)
  • William T. Mallard
    William T. Mallard over 7 years
    Not sure if this still answers the question? As engineers we try to choose the best ( = meets requirements at least expense) solution. In what class of problem is an expression tree the solution of choice?