15

Possible Duplicate:
LINQ equivalent of foreach for IEnumerable<T>

I'm wondering whether there is a method for IEnumerable like the following .Each() in the .Net library

var intArray = new [] {1, 2, 3, 4};
intArrary.Each(Console.WriteLine);

I know I can use a foreach loop or easily write an extension method like this:

public static class EnumerableExtensions
{
    public static void Each<T>(this IEnumerable<T> enumberable, Action<T> action)
    {
        foreach (var item in enumberable)
        {
            action(item);
        }
    }
}

But I'm hoping not to create my own method to mess up code if there is already such an extension method in the library. And something like .Each() (with a few overloadings which can take conditions as extra params) is heavily needed by programmers, and there should already be one. Am I correct?

Update

Ruby developers may recognize it as a .each() iterator. And that's what I hope to have in C#. Maybe C# can have more iterator methods like those in Ruby.

Community
  • 1
  • 1
Shuo
  • 4,749
  • 9
  • 45
  • 63

6 Answers6

10

As others have said there is none built in on IEnumerable<T>. The Linq team was against it as per this post by Eric Lippert:: http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx

There is a static method on Array.ForEach and List<T> has an instance method. There is also in PLINQ foreach like statements, but be warned that they work in parallel and can lead to very bad performance for extremely simple actions. Here is one such method in PLINQ: http://msdn.microsoft.com/en-us/library/dd383744.aspx

And here is a guide on PLINQ in general: http://msdn.microsoft.com/en-us/library/dd460688.aspx

While I can't find the exact article if you poke around in the ParrallelEnumerable section it gives warnings and tips as to how to improve the performance of using parallelism in code

If you want it, I suggest creating 2 versions, one that include indexer and one without. This can be quite useful and can save a select statement to acquire the index. e.g.

public static void ForEach<T>(IEnumerable<T> enumerable,Action<T> action)
{
    foreach(var item in enumerable) action(item);
}

public static void ForEach<T>(IEnumerable<T> enumerable,Action<T,int> action)
{
    int index = 0;
    foreach(var item in enumerable) action(item,index++);
}

I'd also include argument validation as these are public methods.

Michael B
  • 7,512
  • 3
  • 31
  • 57
6

Yes there is.

someList.ForEach(x => Console.WriteLine(x));

Or with an array:

Array.ForEach(someArray, x => Console.WriteLine(x));

Note that in this last example you have to call the static method on the Array class

EDIT: The namespaces are: System.Collections.Generic for the List method and System for the Array method

Raphael
  • 7,972
  • 14
  • 62
  • 83
  • Which namespace should I use? – Shuo Nov 28 '10 at 00:50
  • -1 ForEach is an instance method on `List` - arrays do not have this instance method. – Andrew Hare Nov 28 '10 at 00:51
  • Andrew Hare - Please try to compile the code before voting down. ForEach is also available as a static method for the Array class: http://msdn.microsoft.com/en-us/library/zecdkyw2.aspx – Raphael Nov 28 '10 at 00:52
  • 1
    @Raphael - I removed my downvote because you edited your answer to include `Array.ForEach`. While it isn't an instance or extension method like the OP requested it does get the job done. – Andrew Hare Nov 28 '10 at 00:56
  • Andrew Hare - No harm done :P. I should have included the Array.ForEach in my first answer but the List's ForEach was the first that I could think of – Raphael Nov 28 '10 at 00:58
2

There's a ForEach method on System.Collections.List< T >, but not on IEnumerable. Note, this is also not part of LINQ (it was already there in .NET 2.0).

At first glance you'd expect this to be part of LINQ, as it's sort of an extension on the normal List functionality. However, LINQ is more about grouping, filtering and converting data. If you look closely, most (if not all, I'm not Jon Skeet, I don't know that stuff by heart!) LINQ methods return some sort of IEnumerable.

However, you might want to take a look at MoreLinq. This is an open-source project which adds some key features to the existing LINQ to Objects, one of which is the ForEach method you want (works on any IEnumerable in this case, so it'll work on your array as well).

Erik van Brakel
  • 23,220
  • 2
  • 52
  • 66
  • 1
    Rx includes ForEach for IEnumerable in EnumerableEx ... it will surely become part of the BCL, adding many other operators to IEnumerable, as extension methods, in future versions. – Richard Anthony Hein Nov 28 '10 at 02:13
  • I just double checked and it's not part of the latest version ... whoops. :S I guess they removed it. However, Run and Do will do the same thing, Run returning void and Do returning the input IEnumerable unchanged. – Richard Anthony Hein Nov 28 '10 at 02:21
1

No, there isn't, unfortunately. (I think this question has been discussed before, but I can't find it currently.)

Due to some bizarre accident of history, the ForEach method ended up on List<T>, instead of IEnumerable<T>, where it would make more sense, and because of backwards-compatiblity, this can never ever be fixed.

Ever since extension methods existed, adding a ForEach(this IEnumerable<T>, ...) extension method was requested over and over again, but it is usually rejected because it would lead to confusing behavior: since instance methods are always selected before extension methods, this would mean that all IEnumerables get treated identically, except for Lists and they wouldn't allow such inconsistencies in the BCL.

As a result, pretty much every .NET project on the planet now starts off with exactly the code you described above:

namespace IEnumerableExtensions
{
    public static class IEnumerableExtensions
    {
        public static void ForEach<T>(this IEnumerable<T> xs, Action<T> f)
        {
            foreach (var x in xs) f(x);
        }
    }
}
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • Hmm.. weird. Why not have a name such as .Each() for the method? This is what ruby has. – Shuo Nov 28 '10 at 01:09
  • 1
    @Bryan Shen: You would have to ask the BCL team that :-) I have a couple of ideas, though: consistency with `foreach`, consistency with `Parallel.ForEach` and duplication with `List.ForEach`. – Jörg W Mittag Nov 28 '10 at 01:11
  • 2
    This is completely wrong. The LINQ team wanted to keep it about immutable functionality, and foreach is all about side effect. Nobody would care if the ForEach on list used the indexer rather than the GetNext/MoveNext. – Michael B Nov 28 '10 at 01:12
  • @Jörg W Mittag: Nice. Based on your explanation, I've decided to incorporate into my own library. – Shuo Nov 28 '10 at 01:14
  • 1
    http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx – Michael B Nov 28 '10 at 01:19
  • @Michael B: You are correct, of course. I confused the discussion about adding `ForEach` to the LINQ operators with the discussion about adding a `ForEach` extension method to `IEnumerable`, which are of course two very different things. Indeed, `ForEach` as a LINQ operator *must* be rejected, just for the simple reason that it would break the monadic nature of LINQ. – Jörg W Mittag Nov 28 '10 at 01:53
0

Only List<T> provides a ForEach method - for all other types you will have to roll your own extension method.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • I can't believe this isn't an extension method for IEnumberable – Shuo Nov 28 '10 at 01:01
  • Array has one as the Raphael has pointed out. – TheCodeMonk Nov 28 '10 at 01:03
  • @TheCodeMonk - The `Array` class has a *static method* that operates on only arrays. This is very different from an *extension method* that operates on `IEnumerable`, which seems to be what the OP is really looking for. – Andrew Hare Nov 28 '10 at 01:05
0

it depends on the framework version

.net 3.5 and 4.0 have a ForEach method

Ali Tarhini
  • 5,278
  • 6
  • 41
  • 66