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

Just thinking about the best way to build an Order form that would (from
I have been thinking about a way solve a problem I have and I'm
I have a dilemna that needs I've been thinking a while but still haven't
I have two classes, but don't what kind of relation i should use. I
Thinking about avoiding code replication, I got a question that catches me every time
Thinking about if we modify the definition of Hamiltonian path as we need to
Im thinking about to use a DB for my logs instead of a normal
Im thinking about building an application on our website to track order places through
Been thinking about this for hours now. Im building a simple slideshow application, where
Im thinking about learn to develop app for iOS. I had a lot of

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.