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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:15:33+00:00 2026-05-27T11:15:33+00:00

I ran into what was to me an unexpected result when testing a simple

  • 0

I ran into what was to me an unexpected result when testing a simple ForEach extension method.

ForEach method

public static void ForEach<T>(this IEnumerable<T> list, Action<T> action)
{
    if (action == null) throw new ArgumentNullException("action");

    foreach (T element in list)
    {
        action(element);
    }
}

Test method

[TestMethod]
public void BasicForEachTest()
{
    int[] numbers = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    numbers.ForEach(num =>
    {
        num = 0;
    });

    Assert.AreEqual(0, numbers.Sum());
}

Why would numbers.Sum() be equal to 55 and not 0?

  • 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-27T11:15:34+00:00Added an answer on May 27, 2026 at 11:15 am

    num is the copy of the value of the current element you are iterating over. So you are just changing the copy.

    What you do is basically this:

    foreach(int num in numbers)
    {
         num = 0;
    }
    

    Surely you do not expect this to change the content of the array?

    Edit: What you want is this:

    for (int i in numbers.Length)
    {
         numbers[i] = 0;
    }
    

    In your specific case you could maintain an index in your ForEach extension method and pass that as second argument to the action and then use it like this:

    numbers.ForEachWithIndex((num, index) => numbers[index] = 0);
    

    However in general: Creating Linq style extension methods which modify the collection they are applied to are bad style (IMO). If you write an extension method which cannot be applied to an IEnumerable<T> you should really think hard about it if you really need it (especially when you write with the intention of modifying the collection). You have not much to gain but much to loose (like unexpected side effects). I’m sure there are exceptions but I stick to that rule and it has served me well.

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

Sidebar

Related Questions

I ran into this today when unit testing a generic dictionary. System.Collections.Generic.Dictionary<int, string> actual,
I ran into an unexpected result in round-tripping Int32.MaxValue into a System.Single : Int32
I ran into an interesting (and very frustrating) issue with the equals() method today
I ran into a situation today where Java was not invoking the method I
While creating a map of String to partial functions I ran into unexpected behavior.
Ran into a interesting little problem. I was writing a method to filter an
Ran into this while converting a VB.NET interface to C#; the VB version defines
I ran into unexpected behavior with a function I wrote to compute the average
I've been playing around with Backbone.js and ran into something a bit unexpected. It
I ran into this example for locking Windows workstation: using System.Runtime.InteropServices; ... [DllImport(user32.dll, SetLastError

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.