<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>ForEach</title><link>http://blogs.msdn.com/kirillosenkov/archive/2009/01/31/foreach.aspx</link><description>In my recent post about coding styles one particular thing provoked the majority of feedback and discussions: the ForEach extension method on IEnumerable&amp;lt;T&amp;gt;. Justin Etheredge has a good post about this method here . StackOverflow.com also has a</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: ForEach</title><link>http://blogs.msdn.com/kirillosenkov/archive/2009/01/31/foreach.aspx#9387944</link><pubDate>Sun, 01 Feb 2009 11:00:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9387944</guid><dc:creator>Jon Skeet</dc:creator><description>&lt;p&gt;The final argument looks a bit weaker if you include the quotes round the terms, i.e. looking for the phrase &amp;quot;foreach extension method&amp;quot; - only 273 hits then.&lt;/p&gt;
&lt;p&gt;It's perfectly natural for the word &amp;quot;foreach&amp;quot; to appear with &amp;quot;extension methods&amp;quot;:&lt;/p&gt;
&lt;p&gt;&amp;quot;Extension methods like Select allow you to iterate over a projection with foreach&amp;quot; etc.&lt;/p&gt;
&lt;p&gt;That's not expressing an opinion on the goodness or otherwise of introducing Enumerable.ForEach - just saying that the Google argument doesn't really hold much water IMO.&lt;/p&gt;
&lt;p&gt;Jon&lt;/p&gt;
</description></item><item><title>re: ForEach</title><link>http://blogs.msdn.com/kirillosenkov/archive/2009/01/31/foreach.aspx#9387945</link><pubDate>Sun, 01 Feb 2009 11:01:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9387945</guid><dc:creator>M. Carr</dc:creator><description>&lt;p&gt;My main complaint about the ForEach extension method is that internally (use Reflector) it uses a for-loop to iterate over the collection. This approach allows the user to modify the collection (i.e. add or remove elements) from within the ForEach loop, which messes up the iterator and could unexpectedly cause the &amp;quot;ForEach&amp;quot; to skip elements of the collection. I'm not sure why they used a for-loop within the ForEach, but it could potentially lead to some disasterous and difficult to debug problems.&lt;/p&gt;
</description></item><item><title>re: ForEach</title><link>http://blogs.msdn.com/kirillosenkov/archive/2009/01/31/foreach.aspx#9387970</link><pubDate>Sun, 01 Feb 2009 11:17:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9387970</guid><dc:creator>MichaelGG</dc:creator><description>&lt;p&gt;List&amp;lt;T&amp;gt; already has it (killing the VB argument?). &lt;/p&gt;
&lt;p&gt;State mutation? This is C#. I don't see how having a method versus the foreach keyword is much different.&lt;/p&gt;
&lt;p&gt;Debugging? Anonymous method debugging is something VS2010 better do much better anyways. ForEach would hardly be the only use case there.&lt;/p&gt;
&lt;p&gt;A chaining version can be handy, but it should have another name.&lt;/p&gt;
&lt;p&gt;F# has has &amp;quot;iter&amp;quot; in the Array, List, and Seq modules to achieve the same thing, so I really don't get the &amp;quot;functional&amp;quot; argument. (And &amp;quot;iteri&amp;quot; for the indexed version.)&lt;/p&gt;
&lt;p&gt;My guess is it just didn't &amp;quot;fit in&amp;quot; with querying, hence putting it in &amp;quot;LINQ&amp;quot; doesn't work. But, it doesn't really matter where it goes, because extension methods effectively throw away all that scoping. &lt;/p&gt;
</description></item><item><title>re: ForEach</title><link>http://blogs.msdn.com/kirillosenkov/archive/2009/01/31/foreach.aspx#9388663</link><pubDate>Sun, 01 Feb 2009 23:09:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9388663</guid><dc:creator>Ryan Riley</dc:creator><description>&lt;p&gt;I find it interesting that the Parallel Extensions include both &amp;lt;a href=&amp;quot;&lt;a rel="nofollow" target="_new" href="http://www.danielmoth.com/Blog/2009/01/parallelising-loops-in-net-4.html&amp;quot;&amp;gt;For"&gt;http://www.danielmoth.com/Blog/2009/01/parallelising-loops-in-net-4.html&amp;quot;&amp;gt;For&lt;/a&gt; and ForEach methods&amp;lt;/a&amp;gt; that could easily be added (either internally or via extension methods) to IEnumerable (or just use &amp;lt;a href=&amp;quot;&lt;a rel="nofollow" target="_new" href="http://www.danielmoth.com/Blog/2009/01/plinq.html&amp;quot;&amp;gt;PLINQ&amp;lt;/a&amp;gt;"&gt;http://www.danielmoth.com/Blog/2009/01/plinq.html&amp;quot;&amp;gt;PLINQ&amp;lt;/a&amp;gt;&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;In terms of use, I really liked &amp;lt;a href=&amp;quot;&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/kevinpilchbisson/archive/2008/10/27/removing-elements-from-a-dictionary.aspx&amp;quot;&amp;gt;Kevin"&gt;http://blogs.msdn.com/kevinpilchbisson/archive/2008/10/27/removing-elements-from-a-dictionary.aspx&amp;quot;&amp;gt;Kevin&lt;/a&gt;'s Apply method&amp;lt;/a&amp;gt;, as it appears more as a continuation. However, we must now ask whether we want such a method or whether we want to be more explicit about using &amp;lt;a href=&amp;quot;&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx&amp;quot;&amp;gt;continuations&amp;lt;/a&amp;gt;"&gt;http://blogs.msdn.com/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx&amp;quot;&amp;gt;continuations&amp;lt;/a&amp;gt;&lt;/a&gt; (e.g. with a .ToContinuation() extension), adding the action in the Select method (e.g. &amp;lt;code&amp;gt;list.Select(item =&amp;gt; action(item))&amp;lt;/code&amp;gt;), or an overloaded Select method.&lt;/p&gt;
</description></item><item><title>re: ForEach</title><link>http://blogs.msdn.com/kirillosenkov/archive/2009/01/31/foreach.aspx#9388780</link><pubDate>Mon, 02 Feb 2009 01:02:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9388780</guid><dc:creator>Jared Parsons</dc:creator><description>&lt;p&gt;I definitely think it should be added. &amp;nbsp;My own personal utility library (BclExtensions link below) has a version. &amp;nbsp;I use it constantly it my applications and definately would like to see it added. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;I did end up having to name it Iterate vs. ForEach to avoid conflicts.&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://code.msdn.com/bclextras"&gt;http://code.msdn.com/bclextras&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: ForEach</title><link>http://blogs.msdn.com/kirillosenkov/archive/2009/01/31/foreach.aspx#9388854</link><pubDate>Mon, 02 Feb 2009 02:53:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9388854</guid><dc:creator>Greg Beech</dc:creator><description>&lt;p&gt;I think that you should definitely add two immediately-executed non-chainable overloads because the semantics are clearest (if you return anything other than void then it isn't clear whether the method is lazy or not). The version with an index is also rather useful.&lt;/p&gt;
&lt;p&gt;void ForEach&amp;lt;T&amp;gt;(this IEnumerable&amp;lt;T&amp;gt; source, Action&amp;lt;T&amp;gt; action) { ... }&lt;/p&gt;
&lt;p&gt;void ForEach&amp;lt;T&amp;gt;(this IEnumerable&amp;lt;T&amp;gt; source, Action&amp;lt;T, int&amp;gt; action) { ... }&lt;/p&gt;
&lt;p&gt;Somewhat less useful but occasionally valuable are the param array versions of these. I'm fairly ambivalent about whether these are added but on the other hand they're simple to implement and test, and would provide additional functionality, which is what the BCL is there for.&lt;/p&gt;
&lt;p&gt;void ForEach&amp;lt;T&amp;gt;(this IEnumerable&amp;lt;T&amp;gt; source, params Action&amp;lt;T&amp;gt;[] actions) { ... }&lt;/p&gt;
&lt;p&gt;void ForEach&amp;lt;T&amp;gt;(this IEnumerable&amp;lt;T&amp;gt; source, params Action&amp;lt;T, int&amp;gt;[] actions) { ... }&lt;/p&gt;
</description></item><item><title>re: ForEach</title><link>http://blogs.msdn.com/kirillosenkov/archive/2009/01/31/foreach.aspx#9390154</link><pubDate>Mon, 02 Feb 2009 12:56:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9390154</guid><dc:creator>qrli</dc:creator><description>&lt;p&gt;I think people who want it mainly use it for simply actions like&lt;/p&gt;
&lt;p&gt;collection.ForEach(item -&amp;gt; item.Foo());&lt;/p&gt;
&lt;p&gt;I'm fine with that case except the performance penalty of an extra delegate.&lt;/p&gt;
&lt;p&gt;If you add it to BCL, that means you encourage people to use it. But I don't think that's what you want.&lt;/p&gt;
</description></item><item><title>re: ForEach</title><link>http://blogs.msdn.com/kirillosenkov/archive/2009/01/31/foreach.aspx#9390640</link><pubDate>Mon, 02 Feb 2009 18:25:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9390640</guid><dc:creator>Petar Petrov</dc:creator><description>&lt;p&gt;I think this method is useful and it's definitely worth adding it. I want to be in System.Linq.Enumerable.&lt;/p&gt;
&lt;p&gt;I don't want to be chainable because I believe iterating over the collection is the last thing to do. I've posted my reasoning on my blog (&lt;a rel="nofollow" target="_new" href="http://ppetrov.wordpress.com/2009/01/22/foreach-method-on-ienumerable/"&gt;http://ppetrov.wordpress.com/2009/01/22/foreach-method-on-ienumerable/&lt;/a&gt;)&lt;/p&gt;
</description></item><item><title>re: ForEach</title><link>http://blogs.msdn.com/kirillosenkov/archive/2009/01/31/foreach.aspx#9412302</link><pubDate>Wed, 11 Feb 2009 11:21:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9412302</guid><dc:creator>John Rusk</dc:creator><description>&lt;p&gt;I wanted a ForEach method when I first started using Linq, but since then I've gone off the idea. &amp;nbsp;It just doesn't seem like the C# way. &amp;quot;The C# way&amp;quot; has been the classic foreach keyword for a long time now.&lt;/p&gt;
&lt;p&gt;I've actually found it helpful to tell new developers, &amp;quot;If you want to select stuff, use linq; if you want to mutate state use a foreach (keyword) loop&amp;quot;. &lt;/p&gt;
&lt;p&gt;Also, the ForEach method just doesn't read so well left-to-right. &amp;nbsp;Compare &amp;quot;foo.ForEach(method)&amp;quot; with foo.Where(method)&amp;quot;. &amp;nbsp;Perhaps it partly depends on what you're used to, but foo.Where seems to read well to me, but foo.ForEach doesn't.&lt;/p&gt;
&lt;p&gt;Having said that, I really like the idea that you can use implicit method group conversion, as in foo.ForEach(method). &amp;nbsp;If something is going to be added, what about adding something to the _language_, so that we could write something with still reads with the same left-to-right order as the foreach we are all used to, but with allow method group conversion.&lt;/p&gt;
&lt;p&gt;For instance, let's say that all we want is a shorter, strongly-typed-checked version of the classic foreach loop. Perhaps something like this:&lt;/p&gt;
&lt;p&gt;foreach(myStrings, Console.WriteLine);&lt;/p&gt;
&lt;p&gt;which would be exactly equivalent to this:&lt;/p&gt;
&lt;p&gt;foreach s in myStrings&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Console.WriteLine(s);&lt;/p&gt;
&lt;p&gt;The only difference would be that the version which uses foreach(IEnumerable&amp;lt;T&amp;gt;, Action&amp;lt;T&amp;gt;) could, and should, be type-checked at compile time.&lt;/p&gt;
&lt;p&gt;Of course, you could also write the Action&amp;lt;T&amp;gt; out in lambda style, like this:&lt;/p&gt;
&lt;p&gt;foreach(myStrings, s =&amp;gt; Console.WriteLine(s));&lt;/p&gt;
&lt;p&gt;Overall, this approach keeps the familiar foreach keyword, keeps the familar left-to-right order, and is just as compact as other proposals. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;One concern may be the notion of overloading the syntax of existing keyword (foreach). &amp;nbsp;However, we already have a precedent for such a change: &amp;nbsp;VB.NET recently introduced a function-like version of the &amp;quot;if&amp;quot; statement, as its new ternary operator. &amp;nbsp;The old version of the &amp;quot;if&amp;quot; remains available and works like it always did. &amp;nbsp;Likewise, under the foreach(IEnumerable&amp;lt;T&amp;gt;, Action&amp;lt;T&amp;gt;) proposal, the old version of foreach would remain available and work the same way that it always did.&lt;/p&gt;
</description></item><item><title>re: ForEach</title><link>http://blogs.msdn.com/kirillosenkov/archive/2009/01/31/foreach.aspx#9413188</link><pubDate>Wed, 11 Feb 2009 21:49:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9413188</guid><dc:creator>MichaelGG</dc:creator><description>&lt;p&gt;@John, I agree quite a bit with &amp;quot;It just doesn't seem like the C# way.&amp;quot; Except that the answer isn't &amp;quot;oh well&amp;quot;, but &amp;quot;find something that goes the right way&amp;quot; :)&lt;/p&gt;
</description></item><item><title>ForEach or not ForEach, that is the question</title><link>http://blogs.msdn.com/kirillosenkov/archive/2009/01/31/foreach.aspx#9444407</link><pubDate>Wed, 25 Feb 2009 21:20:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9444407</guid><dc:creator>LINQ in Action - LINQ Book &amp; News</dc:creator><description>&lt;p&gt;In LINQ in Action , we discuss about the missing ForEach query operator. This is in Chapter 5 &amp;quot;Beyond&lt;/p&gt;
</description></item><item><title>ForEach or not ForEach, that is the question</title><link>http://blogs.msdn.com/kirillosenkov/archive/2009/01/31/foreach.aspx#9444411</link><pubDate>Wed, 25 Feb 2009 21:21:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9444411</guid><dc:creator>Fabrice's weblog</dc:creator><description>&lt;p&gt;In LINQ in Action , we discuss about the missing ForEach query operator. This is in Chapter 5 &amp;quot;Beyond&lt;/p&gt;
</description></item><item><title>Why I Don’t Use the ForEach Extension Method</title><link>http://blogs.msdn.com/kirillosenkov/archive/2009/01/31/foreach.aspx#9538394</link><pubDate>Wed, 08 Apr 2009 18:25:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9538394</guid><dc:creator>Eric White's Blog</dc:creator><description>&lt;p&gt;In my first version of my functional programming tutorial , I discussed a ForEach extension method, but&lt;/p&gt;
</description></item></channel></rss>