• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MTY1

Related Code
Invoke Extension Method
Use of Extension methods to hide "infrastructure code"
Generic Entity Framework 4.0 Base Repository
Unity Service Locator for ASP.NET MVC 3.0 Beta 1
Null Dot "Operator" Extension Method
Generic Entity Framework 4.0 Base Repository with Paging
Generic linq-to-sql repository
WCF RIA Services Unity 2.0 DomainService Factory
A Default Entity Framework 4.0 ObjectContext Factory
UnitOfWork Action filter for ASP.NET MVC and nHibernate
StringFormat Extension method for HtmlHelper ASP.NET MVC
Make sure the web.config pages/namespace can be used together with ASP.NET MVC 3 P1 and Razor
IEnumerable<T> Exist extension method
Custom C# exception class
Email validation with Regular Expression
Reflection dynamically get private fields or properties
Url Validation with Regular Expression
Parsing string to enum, extension method
Method to hash passwords
Number of sealed / unsealed types in the framework
Extension to the HttpClient

ForEach Extension method for IEnumerable<T>

IEnumerable doesn't have a ForEach Extension method so here is one.

6
939 0 2 2 1 9

public static class LoopExtensions
{
   public static void ForEach<T>(this IEnumerable<T> values, Action<T> action)
   {
      foreach (var v in values)
         action(v);
   }
}

Share: twitter | facebook   Action: used | fork | flag

.net

Mark '.net' tag as 'like'

Mark '.net' tag as 'ignore'

c#

Mark 'c#' tag as 'like'

Mark 'c#' tag as 'ignore'

extension-method

Mark 'extension-method' tag as 'like'

Mark 'extension-method' tag as 'ignore'

ienumerable

Mark 'ienumerable' tag as 'like'

Mark 'ienumerable' tag as 'ignore'


 @fredrikn "I'm the master"
3.11k
July 10, 2010 9:57 AM
edited July 15, 2010 11:19 PM

Fork

 ForEach Extension method for IEnumerable<T> -  @fredrikn Saturday 10, 2010 9:57 AM


5 Feedback

Fredrik, I believe that this method goes against the spirit of LINQ. -  @luisabreu Sunday 11, 2010 3:35 PM
Can't help it. I just love the syntax of the one-line foreach! -  @abratland Sunday 11, 2010 7:46 PM
Agree with Bratland! -  @CodingInsomnia Sunday 11, 2010 8:04 PM
Basically, this is just another version of the Update extension method that has been around since 2007. -  JonnyBee Tuesday 13, 2010 9:36 AM
How about making this parallel? -  @thoriumi Thursday 15, 2010 2:38 AM

You must log in before you can give any feedback


9 Discussion(s)

Newest Oldest
0

Why do you think it's against the spirit of LINQ? It's just an extension method.. ;)

link | flag  | Reply

 @fredrikn "I'm the master"
3.11k
Sunday 11, 2010 3:46 PM

0

I too like this extension method, but there is a good reason for it not being implemented by default - basically, a LINQ call is supposed to be "functional" in nature without side-effects. The ForEach extension however is pretty much all about the side-effects.

This blog post discusses why in more detail.

But in any case, the extension method is quite useful!

link | flag  | Reply

 @CodingInsomnia "First one to share code"
349
Sunday 11, 2010 8:07 PM

0

The blog post tries to make the extension method harder to read than it is:

Enumerable.ForEach((Foo foo) => { stuff ;});

It would rather be:

Enumerable.ForEach(foo => stuff);

I'm also having a hard time seeing why every single extension method on an IEnumerable must conform to functional style..

Should IEnumerable be protected by specific extension guidelines that other types don't have to conform to?

link | flag  | Reply

 Roggan
15
Monday 12, 2010 1:35 PM

0

No, I totally agree!

link | flag  | Reply

 @CodingInsomnia "First one to share code"
349
Monday 12, 2010 5:06 PM

0

Eric's post explains why MS didn't add the Foreach extension method. Btw, why not:

foreach( var x in collection) { Stuff; }

Isn't this also one line, right? and look: no method invocation. that means it could be faster than the extension method approach...

I guess we should also be careful so that we don't become "extension method alcoholics"...

link | flag  | Reply

 @luisabreu
248
Monday 12, 2010 8:08 PM

0

This is what I would like to have:

ForEach.Collection.Do(Stuff);

I have done it with DynamicObject but the code was ugly.

link | flag  | Reply

 @fredrikn "I'm the master"
3.11k
Monday 12, 2010 8:39 PM

0

There has been an List.ForEach since .NET 2.0. But I to don't feel that this is the right place for an extension methods. I think I just like the functional immutability of the other IEnumerable extensions and also, people are used to the standard foreach(var in xxx) syntax. This, I think, only confuses.

link | flag  | Reply

 @jesperpalm "Code Contributor"
156
Monday 12, 2010 9:11 PM

0

This actually dates back to 2008...

At least now is a good time to join the The “Anti-For” Campaign :-D

link | flag  | Reply

 @thoriumi
781
Tuesday 20, 2010 12:59 PM

1

I will say: It depends on the context and where and how it's used.

link | flag  | Reply

 @fredrikn "I'm the master"
3.11k
Tuesday 20, 2010 2:28 PM


You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010