Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7591639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:42:05+00:00 2026-05-30T20:42:05+00:00

When you are building the expression tree at runtime there’s no code emitted. It’s

  • 0

When you are building the expression tree at runtime there’s no code
emitted. It’s a way to represent .NET code at runtime…

Ok…

Now lets say I have this code :

    ParameterExpression p = Expression.Parameter (typeof (string), "s");
    MemberExpression stringLength = Expression.Property (p, "Length");
    ConstantExpression five = Expression.Constant (5);
    BinaryExpression comparison = Expression.LessThan (stringLength, five);
    Expression<Func<string, bool>> lambda= Expression.Lambda<Func<string, bool>> (comparison, p);

    Func<string, bool> runnable = lambda.Compile();

This code Wont be in IL ? of course it will be ! ( maybe the last line wont emit code until compile …but the first lines I think will emit code !)

So what am i saving here ?

Ok so the first 5 lines did emit code and the last one didn’t… big deal.

What am i missing ? Can you please let me see the whole picture ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:42:06+00:00Added an answer on May 30, 2026 at 8:42 pm

    With an Expression Tree, you build a description of some code instead of the code itself.
    Expression Trees should not be used in the context of writing regular code that ‘shouldn’t be compiled at compile time’. They should be used in more dynamic scenarios.

    The expression tree you show will compile to: s.Length < 5 and you invoke the runnable with bool isStringSmallerThan5 = runnable("MyString").

    The whole idea of Expression Trees is that they describe some code and can be compiled at runtime. This means that you can do the following:

    BinaryExpression comparison = null;
    if (lessThen)
    {
        comparison = Expression.LessThan(stringLength, five);
    }
    else
    {
        comparison = Expression.GreaterThan(stringLength, five);
    }
    

    Now you can change the behavior of your code at runtime!

    The biggest use of Expression Trees is that they can be interpreted by a provider. For example Linq To Entities uses Expression Trees and compiles them to SQL code that can be run against the database. LinqToXml is another example of what you can do with Expression Trees.

    This a nice blog post to get you started.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a rather large Lucene.NET search expression. Is there a best practices way
I'm building an expression tree using discriminated unions. The below code: type IntExpression =
This code works fine in C#: Expression.Lambda(LambdaBody); But none of the methods for building
I'm building a LINQ expression tree but it won't compile because allegedly the local
I created a lambda expression inside my std::for_each call. In it there is code
I'm building a C# expression tree to evaluate simple expressions. The expression strings are
I'm building an expression tree dependency analyzer for a cross data source IQueryProvider. That
I need help on regular expression building. There is a line of Javascript: navigator.userAgent.match(/Firefox\/3./)
Im having problems building a query with the linq to sql data query expression
I am building up a MGrammar spec to parse some pseudo code looking for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.