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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:38:03+00:00 2026-06-03T21:38:03+00:00

Do closures in c# cause code bloat in the generated il? I was told

  • 0

Do closures in c# cause code bloat in the generated il? I was told to avoid lambdas with closure variables as they generate hidden classes in the object file that can store the context for the lambda. A class for every lambda with the closed over variables. Is this true? Or does the compiler reuse an existing class, like Tuple or some internal class?

  • 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-06-03T21:38:05+00:00Added an answer on June 3, 2026 at 9:38 pm

    Extra classes are only generated when they need to be – when you capture variables other than this. However, this isn’t really code bloat in most cases – it’s necessary in order to make the delegate work the way you need it to.

    In some cases you could write more efficient code yourself, but usually to get a delegate with the same effect, you’d end up writing code which was similar to what the compiler would generate for you… but considerably harder to read.

    Most of the time you shouldn’t worry about this sort of “bloat” – avoid micro-optimizing for performance to start with – optimize for readability, and measure the performance instead of guessing about it. Then you can attack the bits of code which really matter, and maybe sacrifice a bit of readability for performance there, when you’ve proved that it’s worth it.

    (Writing modern C# and deliberately avoiding lambda expressions is like trying to code with one hand tied behind your back. If the person advising you is worried about the “bloat” of closures, you could probably give him a heart attack by showing him the state machine generated for async/await in C# 5…)

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

Sidebar

Related Questions

I'm slowly understanding closures more and more, and the following code works. I'm wondering
Possible Duplicate: What is a ‘Closure’? I read more and more about closures but
I was playing with some code to make a "closure like" construct ( not
Consider this code: PS> $timer = New-Object Timers.Timer PS> $timer.Interval = 1000 PS> $i
I have some javascript code (within an object) : toggle: function() { var me
Google Closure Compiler renames all true, false and null occurences in code like; var
Why does this code: function answer(x) { function closure() { var x = x
Taking the following code, Resharper tells me that voicesSoFar and voicesNeededMaximum cause access to
While I was investigating a problem I had with lexical closures in Javascript code,
I want to use some Jython code as a Groovy closure so I believe

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.