Junior · Explain · Expression trees are data and delegates execute

Why are an expression tree and a delegate different values even when they compute the same result?

Think through your answer before opening the explanation.

Runtime & concurrency

Reveal answer and reasoning

An expression tree exposes structured nodes that describe a supported computation, while a delegate is directly callable behavior. The tree can be inspected, validated, or translated before execution; an arbitrary delegate does not provide a general reconstruction of its original source tree. In the lesson, the structure is Add with Multiply on the left, and local execution is requested with an interpretation preference rather than claimed native code generation.

The direct delegate and interpreted tree both produce seven for input three, but that equality does not prove identical cost, allocation, or provider translation. A common wrong answer assumes Compile always means generated native code, or assumes a locally interpretable tree is automatically valid SQL. Choose the representation for the consumer's real need, define any supported node subset, and test an actual provider separately if translation is part of the contract.

Captured data also retains ordinary ownership and lifetime concerns.

What a strong answer covers

  • Trees expose structure; delegates expose invocation.
  • Interpretation and native compilation are distinct.
  • Local execution does not prove provider support.

Common pitfall

Equating the word Compile with runtime native-code generation or universal translation support.

Follow-up questions

  1. Which C# constructs cannot be converted to expression-tree lambdas?
  2. What would a tree validator need to reject?

Self-review

Choose what to revisit. These marks are your own assessment and are separate from lesson completion.

Primary references

Open lesson and code ↗

← All interview questions