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, is there a performance difference between using a JOIN to select rows
Which approach is more preferrable when changing just a single value in model? Aesthetically-speaking
Generally speaking, I am very happy with the changes in Xcode 3.2. However, there
Roughly speaking, what's the performance of .net code that reads/deletes keys from the Windows
I have a question regarding performance. Is there a performance penalty when I call
Generally speaking, the more I use immutable objects in Java the more I'm thinking
My question is mainly about performance. The compiler knows better that, for example, some
Generally speaking, should you do any Browser/Header Caching for dynamic PHP pages? If so,
I have a column in a table which is of type nvarchar(max) and there

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.