Home Dashboard Directory Help

ForEach extension for IEnumerable by Michael_Giagnocavo


Status: 

Closed
 as External Help for as External


47
1
Sign in
to vote
Type: Suggestion
ID: 279093
Opened: 5/30/2007 11:45:09 AM
Access Restriction: Public
1
Workaround(s)
view

Description

Please add a ForEach extension method for IEnumerable. I know it doesn't fit into the idea of a query language, but implementations like LINQ for XML already have similar methods. For instance, System.Xml.Linq.Extensions.Remove<T> returns void and is essentially doing a foreach calling Remove (from the perspective of the user), isn't it?

Also, Array has a static method, and List has an instance method of ForEach so why not IEnumerable?
Details
Sign in to post a comment.
Posted by Thurein on 12/31/2009 at 11:55 PM
ForEach() method for IEnumerable would be very helpful.
Posted by Kirill Osenkov - MSFT on 1/31/2009 at 11:43 PM
I feel that we need to add this because of huge community demand.
Posted by FalconNL2007 on 8/15/2007 at 3:58 AM
Agreed. Having to use ToList or make an extension method every time is rather annoying. Making it a built-in method will save some typing.
Posted by Microsoft on 6/11/2007 at 10:00 AM
Michael,

Thanks for your suggestion. We will consider such a feature for a future version of the Framework. Please also visit http://blogs.msdn.com/bclteam/ for updates and info on Base Class Libraries.

Inbar Gazit
Sign in to post a workaround.
Posted by abatishchev on 9/1/2010 at 2:14 AM
You can always write your own:

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

(c) Me, Eric Lippert http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx and many others independently.