184

There are apparently many ways to iterate over a collection. Curious if there are any differences, or why you'd use one way over the other.

First type:

List<string> someList = <some way to init>
foreach(string s in someList) {
   <process the string>
}

Other Way:

List<string> someList = <some way to init>
someList.ForEach(delegate(string s) {
    <process the string>
});

I suppose off the top of my head, that instead of the anonymous delegate I use above, you'd have a reusable delegate you could specify...

Peter Bruins
  • 513
  • 4
  • 18
Bryce Fischer
  • 5,026
  • 9
  • 27
  • 36
  • 2
    I suggest reading [Eric Lipperts blog "foreach" vs "ForEach"](http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx) – Erik Philips Nov 20 '17 at 22:19

13 Answers13

145

There is one important, and useful, distinction between the two.

Because .ForEach uses a for loop to iterate the collection, this is valid (edit: prior to .net 4.5 - the implementation changed and they both throw):

someList.ForEach(x => { if(x.RemoveMe) someList.Remove(x); }); 

whereas foreach uses an enumerator, so this is not valid:

foreach(var item in someList)
  if(item.RemoveMe) someList.Remove(item);

tl;dr: Do NOT copypaste this code into your application!

These examples aren't best practice, they are just to demonstrate the differences between ForEach() and foreach.

Removing items from a list within a for loop can have side effects. The most common one is described in the comments to this question.

Generally, if you are looking to remove multiple items from a list, you would want to separate the determination of which items to remove from the actual removal. It doesn't keep your code compact, but it guarantees that you do not miss any items.

  • 36
    even then, you should use someList.RemoveAll(x => x.RemoveMe) instead – Mark Cidade Oct 22 '08 at 15:17
  • 3
    With Linq, all things can be done better. I was just showing an example of modifying the collection within foreach... –  Oct 22 '08 at 16:29
  • 2
    RemoveAll() is a method on List. – Mark Cidade Oct 23 '08 at 16:43
  • Noted! Anyhow, modifying a collection during enumeration is verboten, whether you're removing or adding to it. –  Aug 06 '09 at 14:50
  • 3
    You're probably aware of this, but people should beware removing items this way; if you remove item N then iteration will skip over item (N+1) and you won't see it in your delegate or get a chance to remove it, just as if you did this in your own for loop. – El Zorko Apr 25 '11 at 14:13
  • @ElZorko: I think that side effect bites everybody.... I'm editing to add a caveat. –  Apr 25 '11 at 15:52
  • Dude i wanted to ask: can i modify the item of List inside ForEach property? – virusivv Jun 23 '15 at 12:56
  • @virusivv the answer is in the answer –  Jun 23 '15 at 14:15
  • come on, this doesn't work, you can't just put if in ForEach, need {} – Toolkit Feb 03 '16 at 15:04
  • 1
    Starting with .NET 4.5, modifying the collection inside a `List.ForEach` will throw an exception. – Memet Olsen Mar 16 '16 at 13:18
  • 2
    Link for @MemetOlsen's comment about now throwing exceptions: https://msdn.microsoft.com/en-us/library/bwabdf9z(v=vs.110).aspx (tested and confirmed in framework 4.6 via Linqpad) – piers7 Jun 30 '16 at 03:53
  • 10
    If you iterate the list **backwards** with a for loop, you can remove items without any index-shift problems. – S. Tarık Çetin Aug 24 '16 at 22:48
  • @piers7 [archived 20161016](https://web.archive.org/web/20161016055246/https://msdn.microsoft.com/en-us/library/bwabdf9z(v=vs.110).aspx) [archived 2018](https://web.archive.org/web/20180408061825/https://msdn.microsoft.com/en-us/library/bwabdf9z(v=vs.110).aspx) – Кое Кто Jan 11 '21 at 16:31
81

We had some code here (in VS2005 and C#2.0) where the previous engineers went out of their way to use list.ForEach( delegate(item) { foo;}); instead of foreach(item in list) {foo; }; for all the code that they wrote. e.g. a block of code for reading rows from a dataReader.

I still don't know exactly why they did this.

The drawbacks of list.ForEach() are:

  • It is more verbose in C# 2.0. However, in C# 3 onwards, you can use the "=>" syntax to make some nicely terse expressions.

  • It is less familiar. People who have to maintain this code will wonder why you did it that way. It took me awhile to decide that there wasn't any reason, except maybe to make the writer seem clever (the quality of the rest of the code undermined that). It was also less readable, with the "})" at the end of the delegate code block.

  • See also Bill Wagner's book "Effective C#: 50 Specific Ways to Improve Your C#" where he talks about why foreach is preferred to other loops like for or while loops - the main point is that you are letting the compiler decide the best way to construct the loop. If a future version of the compiler manages to use a faster technique, then you will get this for free by using foreach and rebuilding, rather than changing your code.

  • a foreach(item in list) construct allows you to use break or continue if you need to exit the iteration or the loop. But you cannot alter the list inside a foreach loop.

I'm surprised to see that list.ForEach is slightly faster. But that's probably not a valid reason to use it throughout , that would be premature optimisation. If your application uses a database or web service that, not loop control, is almost always going to be be where the time goes. And have you benchmarked it against a for loop too? The list.ForEach could be faster due to using that internally and a for loop without the wrapper would be even faster.

I disagree that the list.ForEach(delegate) version is "more functional" in any significant way. It does pass a function to a function, but there's no big difference in the outcome or program organisation.

I don't think that foreach(item in list) "says exactly how you want it done" - a for(int 1 = 0; i < count; i++) loop does that, a foreach loop leaves the choice of control up to the compiler.

My feeling is, on a new project, to use foreach(item in list) for most loops in order to adhere to the common usage and for readability, and use list.Foreach() only for short blocks, when you can do something more elegantly or compactly with the C# 3 "=>" operator. In cases like that, there may already be a LINQ extension method that is more specific than ForEach(). See if Where(), Select(), Any(), All(), Max() or one of the many other LINQ methods doesn't already do what you want from the loop.

Anthony
  • 4,961
  • 6
  • 62
  • 85
  • Just for curiosity... look at Microsoft implementation... http://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs,0e5a9cf0a310b9e5 – Alexandre Jan 04 '15 at 05:13
17

For fun, I popped List into reflector and this is the resulting C#:

public void ForEach(Action<T> action)
{
    if (action == null)
    {
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
    }
    for (int i = 0; i < this._size; i++)
    {
        action(this._items[i]);
    }
}

Similarly, the MoveNext in Enumerator which is what is used by foreach is this:

public bool MoveNext()
{
    if (this.version != this.list._version)
    {
        ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
    }
    if (this.index < this.list._size)
    {
        this.current = this.list._items[this.index];
        this.index++;
        return true;
    }
    this.index = this.list._size + 1;
    this.current = default(T);
    return false;
}

The List.ForEach is much more trimmed down than MoveNext - far less processing - will more likely JIT into something efficient..

In addition, foreach() will allocate a new Enumerator no matter what. The GC is your friend, but if you're doing the same foreach repeatedly, this will make more throwaway objects, as opposed to reusing the same delegate - BUT - this is really a fringe case. In typical usage you will see little or no difference.

plinth
  • 45,549
  • 9
  • 75
  • 118
  • 1
    You have no guarantee that the code generated by foreach will be the same between compiler versions. The code generated may be improved by a future version. – Anthony Mar 22 '09 at 20:48
  • 2
    As of .NET Core 3.1 ForEach is still faster. – jackmott Jan 31 '20 at 16:20
14

I guess the someList.ForEach() call could be easily parallelized whereas the normal foreach is not that easy to run parallel. You could easily run several different delegates on different cores, which is not that easy to do with a normal foreach.
Just my 2 cents

Paul Chu
  • 1,209
  • 3
  • 16
  • 23
Joachim Kerschbaumer
  • 9,387
  • 7
  • 46
  • 83
  • 2
    I think he meant that the runtime engine could parallelize it automatically. Otherwise, both foreach and .ForEach can be parallelized by hand using a thread from the pool in each action delegate – Isak Savo Oct 24 '08 at 11:37
  • @Isak but that would be an incorrect assumption. If the anonymous method hoists a local or member the runtime will not 8easily) be able to paralellize – Rune FS Jan 13 '12 at 20:44
14

I know two obscure-ish things that make them different. Go me!

Firstly, there's the classic bug of making a delegate for each item in the list. If you use the foreach keyword, all your delegates can end up referring to the last item of the list:

    // A list of actions to execute later
    List<Action> actions = new List<Action>();

    // Numbers 0 to 9
    List<int> numbers = Enumerable.Range(0, 10).ToList();

    // Store an action that prints each number (WRONG!)
    foreach (int number in numbers)
        actions.Add(() => Console.WriteLine(number));

    // Run the actions, we actually print 10 copies of "9"
    foreach (Action action in actions)
        action();

    // So try again
    actions.Clear();

    // Store an action that prints each number (RIGHT!)
    numbers.ForEach(number =>
        actions.Add(() => Console.WriteLine(number)));

    // Run the actions
    foreach (Action action in actions)
        action();

The List.ForEach method doesn't have this problem. The current item of the iteration is passed by value as an argument to the outer lambda, and then the inner lambda correctly captures that argument in its own closure. Problem solved.

(Sadly I believe ForEach is a member of List, rather than an extension method, though it's easy to define it yourself so you have this facility on any enumerable type.)

Secondly, the ForEach method approach has a limitation. If you are implementing IEnumerable by using yield return, you can't do a yield return inside the lambda. So looping through the items in a collection in order to yield return things is not possible by this method. You'll have to use the foreach keyword and work around the closure problem by manually making a copy of the current loop value inside the loop.

More here

Daniel Earwicker
  • 108,589
  • 35
  • 194
  • 274
  • 6
    The problem you mention with `foreach` is "fixed" in C# 5. http://stackoverflow.com/questions/8898925/is-there-a-reason-for-cs-reuse-of-the-variable-in-a-foreach – StriplingWarrior Nov 25 '14 at 18:22
11

As they say, the devil is in the details...

The biggest difference between the two methods of collection enumeration is that foreach carries state, whereas ForEach(x => { }) does not.

But lets dig a little deeper, because there are some things you should be aware of that can influence your decision, and there are some caveats you should be aware of when coding for either case.

Lets use List<T> in our little experiment to observe behavior. For this experiment, I am using .NET 4.7.2:

var names = new List<string>
{
    "Henry",
    "Shirley",
    "Ann",
    "Peter",
    "Nancy"
};

Lets iterate over this with foreach first:

foreach (var name in names)
{
    Console.WriteLine(name);
}

We could expand this into:

using (var enumerator = names.GetEnumerator())
{

}

With the enumerator in hand, looking under the covers we get:

public List<T>.Enumerator GetEnumerator()
{
  return new List<T>.Enumerator(this);
}
    internal Enumerator(List<T> list)
{
  this.list = list;
  this.index = 0;
  this.version = list._version;
  this.current = default (T);
}

public bool MoveNext()
{
  List<T> list = this.list;
  if (this.version != list._version || (uint) this.index >= (uint) list._size)
    return this.MoveNextRare();
  this.current = list._items[this.index];
  ++this.index;
  return true;
}

object IEnumerator.Current
{
  {
    if (this.index == 0 || this.index == this.list._size + 1)
      ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen);
    return (object) this.Current;
  }
}

Two things become immediate evident:

  1. We are returned a stateful object with intimate knowledge of the underlying collection.
  2. The copy of the collection is a shallow copy.

This is of course in no way thread safe. As was pointed out above, changing the collection while iterating is just bad mojo.

But what about the problem of the collection becoming invalid during iteration by means outside of us mucking with the collection during iteration? Best practices suggests versioning the collection during operations and iteration, and checking versions to detect when the underlying collection changes.

Here's where things get really murky. According to the Microsoft documentation:

If changes are made to the collection, such as adding, modifying, or deleting elements, the behavior of the enumerator is undefined.

Well, what does that mean? By way of example, just because List<T> implements exception handling does not mean that all collections that implement IList<T> will do the same. That seems to be a clear violation of the Liskov Substitution Principle:

Objects of a superclass shall be replaceable with objects of its subclasses without breaking the application.

Another problem is that the enumerator must implement IDisposable -- that means another source of potential memory leaks, not only if the caller gets it wrong, but if the author does not implement the Dispose pattern correctly.

Lastly, we have a lifetime issue... what happens if the iterator is valid, but the underlying collection is gone? We now a snapshot of what was... when you separate the lifetime of a collection and its iterators, you are asking for trouble.

Lets now examine ForEach(x => { }):

names.ForEach(name =>
{

});

This expands to:

public void ForEach(Action<T> action)
{
  if (action == null)
    ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
  int version = this._version;
  for (int index = 0; index < this._size && (version == this._version || !BinaryCompatibility.TargetsAtLeast_Desktop_V4_5); ++index)
    action(this._items[index]);
  if (version == this._version || !BinaryCompatibility.TargetsAtLeast_Desktop_V4_5)
    return;
  ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
}

Of important note is the following:

for (int index = 0; index < this._size && ... ; ++index) action(this._items[index]);

This code does not allocate any enumerators (nothing to Dispose), and does not pause while iterating.

Note that this also performs a shallow copy of the underlying collection, but the collection is now a snapshot in time. If the author does not correctly implement a check for the collection changing or going 'stale', the snapshot is still valid.

This doesn't in any way protect you from the problem of the lifetime issues... if the underlying collection disappears, you now have a shallow copy that points to what was... but at least you don't have a Dispose problem to deal with on orphaned iterators...

Yes, I said iterators... sometimes its advantageous to have state. Suppose you want to maintain something akin to a database cursor... maybe multiple foreach style Iterator<T>'s is the way to go. I personally dislike this style of design as there are too many lifetime issues, and you rely on the good graces of the authors of the collections you are relying on (unless you literally write everything yourself from scratch).

There is always a third option...

for (var i = 0; i < names.Count; i++)
{
    Console.WriteLine(names[i]);
}

It ain't sexy, but its got teeth (apologies to Tom Cruise and the movie The Firm)

Its your choice, but now you know and it can be an informed one.

Stacy Dudovitz
  • 616
  • 8
  • 9
5

You could name the anonymous delegate :-)

And you can write the second as:

someList.ForEach(s => s.ToUpper())

Which I prefer, and saves a lot of typing.

As Joachim says, parallelism is easier to apply to the second form.

Craig.Nicol
  • 1,714
  • 2
  • 14
  • 20
4

List.ForEach() is considered to be more functional.

List.ForEach() says what you want done. foreach(item in list) also says exactly how you want it done. This leaves List.ForEach free to change the implementation of the how part in the future. For example, a hypothetical future version of .Net might always run List.ForEach in parallel, under the assumption that at this point everyone has a number of cpu cores that are generally sitting idle.

On the other hand, foreach (item in list) gives you a little more control over the loop. For example, you know that the items will be iterated in some kind of sequential order, and you could easily break in the middle if an item meets some condition.


Some more recent remarks on this issue are available here:

https://stackoverflow.com/a/529197/3043

Joel Coehoorn
  • 362,140
  • 107
  • 528
  • 764
2

Behind the scenes, the anonymous delegate gets turned into an actual method so you could have some overhead with the second choice if the compiler didn't choose to inline the function. Additionally, any local variables referenced by the body of the anonymous delegate example would change in nature because of compiler tricks to hide the fact that it gets compiled to a new method. More info here on how C# does this magic:

http://blogs.msdn.com/oldnewthing/archive/2006/08/04/688527.aspx

jezell
  • 2,522
  • 14
  • 12
2

The ForEach function is member of the generic class List.

I have created the following extension to reproduce the internal code:

public static class MyExtension<T>
    {
        public static void MyForEach(this IEnumerable<T> collection, Action<T> action)
        {
            foreach (T item in collection)
                action.Invoke(item);
        }
    }

So a the end we are using a normal foreach (or a loop for if you want).

On the other hand, using a delegate function is just another way to define a function, this code:

delegate(string s) {
    <process the string>
}

is equivalent to:

private static void myFunction(string s, <other variables...>)
{
   <process the string>
}

or using labda expressions:

(s) => <process the string>
Pablo Caballero
  • 339
  • 2
  • 8
2

The entire ForEach scope (delegate function) is treated as a single line of code (calling the function), and you cannot set breakpoints or step into the code. If an unhandled exception occurs the entire block is marked.

Peter Shen
  • 41
  • 2
1

The second way you showed uses an extension method to execute the delegate method for each of the elements in the list.

This way, you have another delegate (=method) call.

Additionally, there is the possibility to iterate the list with a for loop.

EFrank
  • 1,770
  • 1
  • 23
  • 32
1

One thing to be wary of is how to exit from the Generic .ForEach method - see this discussion. Although the link seems to say that this way is the fastest. Not sure why - you'd think they would be equivalent once compiled...

Chris Kimpton
  • 5,358
  • 6
  • 43
  • 71