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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:10:43+00:00 2026-05-26T16:10:43+00:00

speaking on performance level which is more preferred to be used and is lighter

  • 0

speaking on performance level which is more preferred to be used and is lighter in terms of compiler work and there any major differences?

  List<int> intList;
  foreach (int i in intList)

or

intList.ForEach(i => result += i);
  • 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-26T16:10:44+00:00Added an answer on May 26, 2026 at 4:10 pm

    TL;DR: The performance difference here is almost certainly insignificant in a real application, and there’s a more readable way of achieving the same result anyway. It’s still interesting to see the differences in compiled code though.

    Assuming the full code is actually:

    int result = 0;
    foreach (int i in intList)
    {
        result += i;
    }
    

    vs

    int result = 0;
    intList.ForEach(i => result += i);
    

    then the first form is rather simpler in terms of what gets generated – you’ll end up with just a local variable, code to iterate over the list (using List<T>.Enumerator) and IL which adds the value to the local variable.

    The second form will need to generate a new class with an instance variable for result, along with a method to be used as the delegate. The code will be converted to:

    CompilerGeneratedClass tmp = new CompilerGeneratedClass();
    tmp.result = 0;
    Action<int> tmpDelegate = new Action<int>(tmp.CompilerGeneratedMethod);
    intList.ForEach(tmpDelegate);
    

    On top of that there’s the philosophical differences between foreach and ForEach which Eric Lippert has written about.

    Personally I’d just use LINQ though:

    int result = intList.Sum();
    

    I doubt that the performance differences will actually be a bottleneck in real code, but the LINQ version is the clearest IMO, and that’s always a good thing.

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

Sidebar

Related Questions

I realize that, generally speaking, there are performance implications of using reflection. (I myself
Generally speaking, any ant task which accepts a <mapper> will also accept several tags
Which approach is more preferrable when changing just a single value in model? Aesthetically-speaking
Speaking entirely in technology-free terms, what is the best way to make a mobile
Generally speaking, I am very happy with the changes in Xcode 3.2. However, there
I have a strange performance problem with a query used to create a filter
Internally speaking, which algorithm(s) does PHP use to implement the various sort functions it
Frankly speaking. I'm looking for a specific video course webpage which I visited few
I have a question regarding performance. Is there a performance penalty when I call
Roughly speaking I am looking for a more elegant alternative for this snippet: #

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.