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

  • Home
  • SEARCH
  • 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 591927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:41:10+00:00 2026-05-13T15:41:10+00:00

I am thinking about a re-factor, but I don’t know if the end result

  • 0

I am thinking about a re-factor, but I don’t know if the end result is just overkill.
Currently I have

    IList<myobjecttypebase> m_items;

   public int GetIncrementTotal()
   {
     int incrementTot;
     foreach(myobjecttypebase x in m_items)
     { incrementTot += x.GetIncrement(); }
   }

Would it be overkill and/or less efficient to use linq for the ForEach

m_items.ToList().ForEach(x => incrementTot += x.GetIncrement());

Would the cast be a significant overhead here?

  • 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-13T15:41:11+00:00Added an answer on May 13, 2026 at 3:41 pm

    The ToList method is an extension method used with LINQ, but the ForEach method is just a method in the List class.

    The major overhead here is the call to the ToList method, which creates a new List from the collection. The ForEach also has a slight overhead as it has to call a delegate for each item.

    If you want to use LINQ methods, the Aggregate method seems more appropriate:

    public int GetIncrementTotal() {
      return m_items.Aggregate(0, (t, i) => t + i.GetIncrement());
    }
    

    Or Sum:

    public int GetIncrementTotal() {
      return m_items.Sum(i => i.GetIncrement());
    }
    

    Either has a slight overhead over your original version, so if you want the most efficient, just stick to a simple loop.

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

Sidebar

Related Questions

I just started thinking about creating/customizing a web crawler today, and know very little
I am currently thinking about some efficiency/speed issues. I have about 40 a elements
Without thinking about it at all I just want to say I should allow
Currently thinking about pitching the argument for us migration from vs 2005 (winforms) to
Thinking about getting into .net technology project management I've had plenty of experience with
Thinking about a Windows-hosted build process that will periodically drop files to disk to
Thinking about my other problem , i decided I can't even create a regular
While thinking about this question and conversing with the participants, the idea came up
I’m thinking about trying some development for the iPhone, is it possible to install
I am thinking about providing a version of say, the database schema and the

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.