<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Observations on the &#8216;if&#8217; statement</title>
	<atom:link href="http://elegantcode.com/2009/08/14/observations-on-the-if-statement/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com/2009/08/14/observations-on-the-if-statement/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=observations-on-the-if-statement</link>
	<description></description>
	<lastBuildDate>Sun, 12 Feb 2012 18:54:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
	<item>
		<title>By: C# test.net &#187; Ahhh, alas I&#8217;m not the only one who hates the &#8216;if&#8217; keyword</title>
		<link>http://elegantcode.com/2009/08/14/observations-on-the-if-statement/comment-page-3/#comment-48881</link>
		<dc:creator>C# test.net &#187; Ahhh, alas I&#8217;m not the only one who hates the &#8216;if&#8217; keyword</dc:creator>
		<pubDate>Thu, 27 Aug 2009 23:06:48 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/08/14/observations-on-the-if-statement/#comment-48881</guid>
		<description>[...] Excellent job guys and mad respect for giving this issue it&#8217;s very own online site/campaign. Join me and the many others to move the community in a positive direction. BTW, I found this indirectly on a great post about the subject by elegantcode.com. [...]</description>
		<content:encoded><![CDATA[<p>[...] Excellent job guys and mad respect for giving this issue it&#8217;s very own online site/campaign. Join me and the many others to move the community in a positive direction. BTW, I found this indirectly on a great post about the subject by elegantcode.com. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Please, it is just a &#8216;var&#8217; &#124; Elegant Code</title>
		<link>http://elegantcode.com/2009/08/14/observations-on-the-if-statement/comment-page-3/#comment-48822</link>
		<dc:creator>Please, it is just a &#8216;var&#8217; &#124; Elegant Code</dc:creator>
		<pubDate>Wed, 26 Aug 2009 03:33:26 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/08/14/observations-on-the-if-statement/#comment-48822</guid>
		<description>[...] on a previous post I had a little bit of code with a ‘var’ statement in it.&#160; Actually, there were a few other [...]</description>
		<content:encoded><![CDATA[<p>[...] on a previous post I had a little bit of code with a ‘var’ statement in it.&#160; Actually, there were a few other [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bangoker</title>
		<link>http://elegantcode.com/2009/08/14/observations-on-the-if-statement/comment-page-2/#comment-48821</link>
		<dc:creator>bangoker</dc:creator>
		<pubDate>Wed, 26 Aug 2009 01:49:44 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/08/14/observations-on-the-if-statement/#comment-48821</guid>
		<description>&lt;a href=&quot;#comment-48532&quot; rel=&quot;nofollow&quot;&gt;@Steve Py&lt;/a&gt; 
Well about the var vs explicit type... its the same once its compiled. &quot;var&quot; is just syntactic sugar that tells the compiler to find out what is the type being used in the right side of the operation, so when its compiled both:
 int a = 5 and var a = 5 will compile to int a = 5</description>
		<content:encoded><![CDATA[<p><a href="#comment-48532" rel="nofollow">@Steve Py</a><br />
Well about the var vs explicit type&#8230; its the same once its compiled. &#8220;var&#8221; is just syntactic sugar that tells the compiler to find out what is the type being used in the right side of the operation, so when its compiled both:<br />
 int a = 5 and var a = 5 will compile to int a = 5</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Maxim&#8217;s blog &#187; links for 2009-08-18</title>
		<link>http://elegantcode.com/2009/08/14/observations-on-the-if-statement/comment-page-2/#comment-48537</link>
		<dc:creator>Maxim&#8217;s blog &#187; links for 2009-08-18</dc:creator>
		<pubDate>Tue, 18 Aug 2009 07:02:46 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/08/14/observations-on-the-if-statement/#comment-48537</guid>
		<description>[...] Observations on the ‘if’ statement &#124; Elegant Code [...]</description>
		<content:encoded><![CDATA[<p>[...] Observations on the ‘if’ statement | Elegant Code [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Py</title>
		<link>http://elegantcode.com/2009/08/14/observations-on-the-if-statement/comment-page-2/#comment-48533</link>
		<dc:creator>Steve Py</dc:creator>
		<pubDate>Tue, 18 Aug 2009 02:12:15 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/08/14/observations-on-the-if-statement/#comment-48533</guid>
		<description>Arg, stunng by the &lt; &gt; bug as well. that should read OfType&lt;B&gt;  :)</description>
		<content:encoded><![CDATA[<p>Arg, stunng by the &lt; &gt; bug as well. that should read OfType&lt;B&gt;  <img src='http://elegantcode.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Py</title>
		<link>http://elegantcode.com/2009/08/14/observations-on-the-if-statement/comment-page-2/#comment-48532</link>
		<dc:creator>Steve Py</dc:creator>
		<pubDate>Tue, 18 Aug 2009 02:10:37 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/08/14/observations-on-the-if-statement/#comment-48532</guid>
		<description>&lt;a href=&quot;#comment-48522&quot; rel=&quot;nofollow&quot;&gt;@Daniel Earwicker &lt;/a&gt; 
OfType, cool! I didn&#039;t spot that one. I just did a quick whirr and it handles A : B : C nicely where OfType&lt;B&gt; returns B&#039;s &amp; A&#039;s.

@Bill, um, not sure I get whether you got my original point or not. Lists are immutable within a loop (I.e. an exception if you try to add while inside a foreach), but the elements within that collection can be modified. The difference here is that if you use the LINQ expression inside the foreach statement and change an element that hasn&#039;t been iterated through yet while in the loop, the linq expression will pick up that change as it is evaluated for each element. It is not evaluated once, creating a new list of results to iterate through, so there is a difference in behaviour between whether you retrieve the IEnumerable outside of the loop, or use LINQ inside the loop. It was thought there might be a performance hit with that, but it turns out there isn&#039;t. (In fact it looks like there is a small savings)  

I&#039;ve set up a set of tests to check performance between the foreach using var vs. cast with the LINQ query in the foreach vs. done separately. Essentially I added 10000 elements to a list with the property to get the element number pausing for 1ms. The LINQ statement returns items MOD 5 (returning 2005 items)
I ran the tests in different orders several times and the results were virtually identical within a 2% variance. The curious thing was that performing the query outside of the foreach was typically that 2% slower than both tests with the LINQ performed in the foreach. There was no difference between using var vs. explicit type specification within the foreach.</description>
		<content:encoded><![CDATA[<p><a href="#comment-48522" rel="nofollow">@Daniel Earwicker </a><br />
OfType, cool! I didn&#8217;t spot that one. I just did a quick whirr and it handles A : B : C nicely where OfType<b> returns B&#8217;s &amp; A&#8217;s.</p>
<p>@Bill, um, not sure I get whether you got my original point or not. Lists are immutable within a loop (I.e. an exception if you try to add while inside a foreach), but the elements within that collection can be modified. The difference here is that if you use the LINQ expression inside the foreach statement and change an element that hasn&#8217;t been iterated through yet while in the loop, the linq expression will pick up that change as it is evaluated for each element. It is not evaluated once, creating a new list of results to iterate through, so there is a difference in behaviour between whether you retrieve the IEnumerable outside of the loop, or use LINQ inside the loop. It was thought there might be a performance hit with that, but it turns out there isn&#8217;t. (In fact it looks like there is a small savings)  </p>
<p>I&#8217;ve set up a set of tests to check performance between the foreach using var vs. cast with the LINQ query in the foreach vs. done separately. Essentially I added 10000 elements to a list with the property to get the element number pausing for 1ms. The LINQ statement returns items MOD 5 (returning 2005 items)<br />
I ran the tests in different orders several times and the results were virtually identical within a 2% variance. The curious thing was that performing the query outside of the foreach was typically that 2% slower than both tests with the LINQ performed in the foreach. There was no difference between using var vs. explicit type specification within the foreach.</b></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bill Sorensen</title>
		<link>http://elegantcode.com/2009/08/14/observations-on-the-if-statement/comment-page-2/#comment-48526</link>
		<dc:creator>Bill Sorensen</dc:creator>
		<pubDate>Mon, 17 Aug 2009 18:37:26 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/08/14/observations-on-the-if-statement/#comment-48526</guid>
		<description>&lt;blockquote cite=&quot;#commentbody-48496&quot;&gt;
&lt;strong&gt;&lt;a href=&quot;#comment-48496&quot; rel=&quot;nofollow&quot;&gt;Steve Py&lt;/a&gt; :&lt;/strong&gt;
Yes, the Lambda rule would be evaluated each iteration in that example.
&lt;/blockquote&gt;

Um...no.

void Main()
{
	var list = new[] { new Animal{Name = &quot;Bulldog&quot;}, new Animal{Name = &quot;Leopard&quot;}, new Animal{Name = &quot;Doberman&quot;} };
			
	foreach (var data in list.Where(x =&gt; x.IsDog))
	{
		Console.WriteLine(data.ToString());
	}
		
}

class Animal
{
	public string Name { get; set; }
	public bool IsDog
	{
		get
		{
			Console.WriteLine(&quot;IsDog called&quot;);
			return Name != &quot;Leopard&quot;;
		}
	}
	public override string ToString() { return Name; }
}


Output:

IsDog called
Bulldog
IsDog called
IsDog called
Doberman</description>
		<content:encoded><![CDATA[<blockquote cite="#commentbody-48496"><p>
<strong><a href="#comment-48496" rel="nofollow">Steve Py</a> :</strong><br />
Yes, the Lambda rule would be evaluated each iteration in that example.
</p></blockquote>
<p>Um&#8230;no.</p>
<p>void Main()<br />
{<br />
	var list = new[] { new Animal{Name = &#8220;Bulldog&#8221;}, new Animal{Name = &#8220;Leopard&#8221;}, new Animal{Name = &#8220;Doberman&#8221;} };</p>
<p>	foreach (var data in list.Where(x =&gt; x.IsDog))<br />
	{<br />
		Console.WriteLine(data.ToString());<br />
	}</p>
<p>}</p>
<p>class Animal<br />
{<br />
	public string Name { get; set; }<br />
	public bool IsDog<br />
	{<br />
		get<br />
		{<br />
			Console.WriteLine(&#8220;IsDog called&#8221;);<br />
			return Name != &#8220;Leopard&#8221;;<br />
		}<br />
	}<br />
	public override string ToString() { return Name; }<br />
}</p>
<p>Output:</p>
<p>IsDog called<br />
Bulldog<br />
IsDog called<br />
IsDog called<br />
Doberman</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dew Drop &#8211; August 15, 2009 &#124; Alvin Ashcraft's Morning Dew</title>
		<link>http://elegantcode.com/2009/08/14/observations-on-the-if-statement/comment-page-2/#comment-48524</link>
		<dc:creator>Dew Drop &#8211; August 15, 2009 &#124; Alvin Ashcraft's Morning Dew</dc:creator>
		<pubDate>Mon, 17 Aug 2009 13:24:11 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/08/14/observations-on-the-if-statement/#comment-48524</guid>
		<description>[...] Observations on the ‘if’ statement (Chris Brandsma) [...]</description>
		<content:encoded><![CDATA[<p>[...] Observations on the ‘if’ statement (Chris Brandsma) [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Earwicker</title>
		<link>http://elegantcode.com/2009/08/14/observations-on-the-if-statement/comment-page-2/#comment-48522</link>
		<dc:creator>Daniel Earwicker</dc:creator>
		<pubDate>Mon, 17 Aug 2009 12:15:05 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/08/14/observations-on-the-if-statement/#comment-48522</guid>
		<description>By which of course I meant:

foreach (Dog dog in animalList.OfType&lt;Dog&gt;())

Curse this angle-bracket HTML/generics confusion!</description>
		<content:encoded><![CDATA[<p>By which of course I meant:</p>
<p>foreach (Dog dog in animalList.OfType&lt;Dog&gt;())</p>
<p>Curse this angle-bracket HTML/generics confusion!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Earwicker</title>
		<link>http://elegantcode.com/2009/08/14/observations-on-the-if-statement/comment-page-2/#comment-48521</link>
		<dc:creator>Daniel Earwicker</dc:creator>
		<pubDate>Mon, 17 Aug 2009 12:12:53 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/08/14/observations-on-the-if-statement/#comment-48521</guid>
		<description>Also, converting to a List doesn&#039;t avoid the performance hit per iteration. It just does the iteration twice and takes an additional performance hit of allocating a copy of the list.</description>
		<content:encoded><![CDATA[<p>Also, converting to a List doesn&#8217;t avoid the performance hit per iteration. It just does the iteration twice and takes an additional performance hit of allocating a copy of the list.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

