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 7127893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:04:52+00:00 2026-05-28T11:04:52+00:00

What are C# lambda’s compiled into? A stackframe, an instance of an anonymous type,

  • 0

What are C# lambda’s compiled into? A stackframe, an instance of an anonymous type, or?

I’ve read this question. Which mostly answers “why” you can’t use a lambda when also using implicit type features. But, this question is aimed at answering what construct the compiler produces to actually carry out the code of a lambda. Is it a method call of an anonymous type (something like anonymous types that implement an interface in Java?) or is it just a stack frame with references to closed variables and the accepting the parameter signature? Some lambda’s don’t close over anything — so are there then 2 different resulting outputs from the compile.

  • 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-28T11:04:52+00:00Added an answer on May 28, 2026 at 11:04 am

    Assuming you mean “as a delegate”, then it still depends :p if it captures any variables (including “this”, which may be implicit) then those variables are actually implemented as fields on a compiler-generated type (not exposed anywhere public), and the statement body becomes a method on that capture class. If there are multiple levels of capture, the outer capture is again a field on the inner capture class. But essentially:

    int i = ...
    Func<int,int> func = x => 2*x*i;
    

    Is like;

    var capture = new SecretType();
    capture.i = ...
    Func<int,int> func = capture.SecretMethod;
    

    Where:

    class SecretType {
        public int i;
        public int SecretMethod(int x) { return 2*x*i; }
    }
    

    This is identical to “anonymous methods”, but with different syntax.

    Note that methods that do not capture state may be implemented as static methods without a capture class.

    Expression trees, on the other hand… Are trickier to explain :p

    But (I don’t have a compiler to hand, so bear with me):

    int i = ...
    Expression<Func<int,int>> func = x => 2*x*i;
    

    Is something like:

    var capture = new SecretType();
    capture.i = ...
    var p = Expression.Parameter("x", typeof(int));  
    Expression<Func<int,int>> func = Expression.Lambda<Func<int,int>>(
        Expression.Multiply(
            Expression.Multiply(Expression.Constant(2),p),
            Expression.PropertyOrField(Expression.Constant(capture), "i")
        ), p);
    

    (except using the non-existent “memberof” construct, since the compiler can cheat)

    Expression trees are complex, but can be deconstructed and inspected – for example to translate into TSQL.

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

Sidebar

Related Questions

Possible Duplicate: C# Lambda ( => ) For instance Messenger.Default.Register<AboutToCloseMessage>(this, (msg) => { if
Can lambda expressions be used as class template parameters? (Note this is a very
Hi I am trying to use Lambda Expression to cast anonymous type list to
Can NHibernate linq/lambda expressions be compiled so they aren't reevaluate on every use?
I have this lambda expression Expression<Func<bool>> commandToExecute Then I pass an instance of a
Question: ((lambda (x y) (x y)) (lambda (x) (* x x)) (* 3 3))
I've read that Lambda Expressions are an incredibly powerful addition to C#, yet I
I mostly use lambda functions but sometimes use nested functions that seem to provide
I understand what are lambda functions in Python, but I can't find what is
Why does this lambda expression not compile? Action a = () => throw new

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.