69

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.

johnnyRose
  • 7,310
  • 17
  • 40
  • 61
Donald N. Mafa
  • 5,131
  • 10
  • 39
  • 56
  • 1
    possible duplicate of [Why would you use Expression> rather than Func?](http://stackoverflow.com/questions/793571/why-would-you-use-expressionfunct-rather-than-funct) – nawfal Jul 07 '14 at 06:52
  • Answered with few examples ..http://stackoverflow.com/questions/403088/practical-use-of-expression-trees/20470060#20470060 – Moumit Apr 25 '16 at 11:29

1 Answers1

67

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

Gusdor
  • 14,001
  • 2
  • 52
  • 64
Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
  • 4
    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. – david.pfx Jun 29 '16 at 05:53
  • 3
    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. – Roy Dictus Jul 13 '16 at 14:10
  • 2
    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. – david.pfx Jul 15 '16 at 00:23
  • @david.pfx I would expect readers with the intelligence of software developers to understand that from my answer. – Roy Dictus Jul 19 '16 at 09:44
  • 1
    @RoyDictus It is not obvious without reading between the lines. Assuming intelligence is often a bad idea. Please be explicit. – Gusdor Jul 29 '16 at 11:23
  • 4
    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) – Steve Byrne Sep 08 '16 at 17:05
  • 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? – William T. Mallard Dec 15 '16 at 23:01