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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:48:37+00:00 2026-05-24T22:48:37+00:00

Does using a lambda expression generate garbage for the GC opposed to the normal

  • 0

Does using a lambda expression generate garbage for the GC opposed to the normal foreach loop?

// Lambda version
Foos.ForEach(f=>f.Update(gameTime));

// Normal approach:
foreach (Foo f in Foos)
{
  f.Update(gameTime);
}

The CLR profiler shows that I have 69.9% system.Action< T > and I suspect that being the lamba version of the foreach loop as above. Is that true?

EDIT: I used the Microsoft CLR profiler: http://download.microsoft.com/download/4/4/2/442d67c7-a1c1-4884-9715-803a7b485b82/clr%20profiler.exe
or http://msdn.microsoft.com/en-us/library/ff650691.aspx

  • 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-24T22:48:39+00:00Added an answer on May 24, 2026 at 10:48 pm

    Yes, a lambda will create garbage if the closure captures a variable from the local scope (i.e. gameTime in this context).

    For example, the following C# function:

    static void TestLambda(List<Foo> Foos, DateTime gameTime)
    {
        Foos.ForEach(f => f.Update(gameTime));
    }
    

    Will get translated to this:

    private static void TestLambda(List<Foo> Foos, DateTime gameTime)
    {
        Program.<>c__DisplayClass1 <>c__DisplayClass = new Program.<>c__DisplayClass1();
        <>c__DisplayClass.gameTime = gameTime;
        Foos.ForEach(new Action<Foo>(<>c__DisplayClass.<TestLambda>b__0));
    }
    

    Note that there are two instances of new in the resulting code, meaning that there is not only Action objects being allocated (the closures), but also objects to hold the captured variables (escaping variable records).

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

Sidebar

Related Questions

I'm new in using lambda expression sorry for a dumb question. Anyway consider this
There are a lot of benefits for using lambda expression to capture property or
What types and arguments does the method, Any when using Expression.Call take? I have
Going from a lambda to an Expression is easy using a method call... public
I know that this can be rewritten using a lambda expression. But I cant
For various reasons I'm constructing a C# lambda dynamically using the expression tree facilities.
This lambda does not compile, but I do not understand why. using System; using
Does using Multiple .NET Languages (Delphi, VB.NET, C#) into the same application (but in
Does using Service Oriented Architecture (SOA) in a system mean it is a distributed
When does using extension methods make sense? Does adding extension methods to a type

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.