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

C#.NetLinqExpression Trees

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDonald N. MafaView Question on Stackoverflow
Solution 1 - C#Roy DictusView Answer on Stackoverflow