<?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 keyword or ForEach&amp;lt;T&amp;gt; extension method?</title><link>http://blogs.msdn.com/simonince/archive/2009/04/27/foreach-keyword-or-foreach-t-extension-method.aspx</link><description>Is this a question of taste, or something more serious? [ Warning : this post won’t change your life J ] I found myself writing the following extension method yet again the other day; public static void ForEach&amp;lt;T&amp;gt;( this IEnumerable &amp;lt;T&amp;gt; source,</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: foreach keyword or ForEach&lt;T&gt; extension method?</title><link>http://blogs.msdn.com/simonince/archive/2009/04/27/foreach-keyword-or-foreach-t-extension-method.aspx#9571162</link><pubDate>Mon, 27 Apr 2009 19:22:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9571162</guid><dc:creator>Richard Collette</dc:creator><description>&lt;p&gt;From a performance standpoint it matters. &amp;nbsp;Lambdas are essentially delegates and do not perform as well as a normal loop structure. &amp;nbsp;Test it.&lt;/p&gt;
&lt;p&gt;At best you're only saving a few keystrokes, maybe none depending on what intellisense does for you between the two versions.&lt;/p&gt;
&lt;p&gt;This is trying to solve a problem that doesn't exist. &amp;nbsp; I will admit that I sometimes take the slight performance hit and Linq/Lambdas when it makes the code more readable. But in critical loops and where the Linq code may actually read more complex, I will use a normal loop construct.&lt;/p&gt;</description></item><item><title>foreach keyword or ForEach extension method? | ASP NET Hosting</title><link>http://blogs.msdn.com/simonince/archive/2009/04/27/foreach-keyword-or-foreach-t-extension-method.aspx#9571171</link><pubDate>Mon, 27 Apr 2009 19:25:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9571171</guid><dc:creator>foreach keyword or ForEach extension method? | ASP NET Hosting</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://asp-net-hosting.simplynetdev.com/foreach-keyword-or-foreach-extension-method/"&gt;http://asp-net-hosting.simplynetdev.com/foreach-keyword-or-foreach-extension-method/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: foreach keyword or ForEach&lt;T&gt; extension method?</title><link>http://blogs.msdn.com/simonince/archive/2009/04/27/foreach-keyword-or-foreach-t-extension-method.aspx#9571745</link><pubDate>Mon, 27 Apr 2009 22:21:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9571745</guid><dc:creator>simonince</dc:creator><description>&lt;p&gt;@ Richard,&lt;/p&gt;
&lt;p&gt;Good points - and a balanced approach I think. &lt;/p&gt;
&lt;p&gt;For most code I write, though, the overhead of using delegates/lambdas is worthwhile for code readability... unless they're used so much that readability actually gets worse, which I guess is the point of the post!&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;
</description></item><item><title>re: foreach keyword or ForEach&lt;T&gt; extension method?</title><link>http://blogs.msdn.com/simonince/archive/2009/04/27/foreach-keyword-or-foreach-t-extension-method.aspx#9572957</link><pubDate>Tue, 28 Apr 2009 07:48:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9572957</guid><dc:creator>MichaelGG</dc:creator><description>&lt;p&gt;I'm lucky I guess that most of my perf can be found with better algorithms. I can only recall a few times where the overhead of delegate invocation was an issue, and once of those was a bug in the CLR. &lt;/p&gt;
&lt;p&gt;foreach has overhead too, versus a for loop and manual indexing. Maybe the compiler optimizes it on some types, but I don't believe it does on any IList. &lt;/p&gt;
&lt;p&gt;In F#, I write much less efficient code, and it's very rarely a problem. If I do profile and find a certain path is slow, fine, move back to mutables and even pointers if necesary. But I highly doubt that many people work in application where these sort of optimizations are needed all the time...&lt;/p&gt;
</description></item><item><title>re: foreach keyword or ForEach&lt;T&gt; extension method?</title><link>http://blogs.msdn.com/simonince/archive/2009/04/27/foreach-keyword-or-foreach-t-extension-method.aspx#9573238</link><pubDate>Tue, 28 Apr 2009 11:30:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9573238</guid><dc:creator>simonince</dc:creator><description>&lt;p&gt;@ Michael;&lt;/p&gt;
&lt;p&gt;Ditto - every now and then some code needs low level tuning, but I prioritise maintainability first by default... generally it is architectural decisions that impact perf more in the fields I do most work in.&lt;/p&gt;
&lt;p&gt;But what's interesting is that you use F# - so you're used to the functional paradigm... as am I from quite some time ago, but others may not be. I wonder if this makes you (and I) more accepting of lambdas etc as programming constructs as it is just a natural fit?&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;
</description></item><item><title>re: foreach keyword or ForEach&lt;T&gt; extension method?</title><link>http://blogs.msdn.com/simonince/archive/2009/04/27/foreach-keyword-or-foreach-t-extension-method.aspx#9573422</link><pubDate>Tue, 28 Apr 2009 14:24:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9573422</guid><dc:creator>MichaelGG</dc:creator><description>&lt;p&gt;F# introduced me to the functional mindset, but before I could use F#, I had a couple of years of full time C# (and still have a lot of C# still).&lt;/p&gt;
&lt;p&gt;The fact C# came out with a nice intro FP feature set with 3.0 was a sanity-saving bonus :). &lt;/p&gt;
&lt;p&gt;Personal experience tells me as soon as a C# programmer &amp;quot;gets it&amp;quot; -- they grasp they can toss around functions -- they tend to love it and use it tons. Anyone who likes LINQ is pretty much validating that, don't you think?&lt;/p&gt;</description></item></channel></rss>