<?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"
	>
<channel>
	<title>Comments on: The Yield Statement in C#</title>
	<atom:link href="http://www.alteridem.net/2007/08/22/the-yield-statement-in-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alteridem.net/2007/08/22/the-yield-statement-in-c/</link>
	<description>Software by Design</description>
	<pubDate>Fri, 04 Jul 2008 03:18:00 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Lars Corneliussen</title>
		<link>http://www.alteridem.net/2007/08/22/the-yield-statement-in-c/#comment-2103</link>
		<dc:creator>Lars Corneliussen</dc:creator>
		<pubDate>Tue, 10 Jun 2008 21:40:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.alteridem.net/2007/08/22/the-yield-statement-in-c/#comment-2103</guid>
		<description>If you're interested in how the yield keyword actually works, read this:

http://startbigthinksmall.wordpress.com/2008/06/09/behind-the-scenes-of-the-c-yield-keyword/</description>
		<content:encoded><![CDATA[<p>If you&#8217;re interested in how the yield keyword actually works, read this:</p>
<p><a href="http://startbigthinksmall.wordpress.com/2008/06/09/behind-the-scenes-of-the-c-yield-keyword/" rel="nofollow">http://startbigthinksmall.wordpress.com/2008/06/09/behind-the-scenes-of-the-c-yield-keyword/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Brenchley</title>
		<link>http://www.alteridem.net/2007/08/22/the-yield-statement-in-c/#comment-1158</link>
		<dc:creator>David Brenchley</dc:creator>
		<pubDate>Fri, 24 Aug 2007 14:44:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.alteridem.net/2007/08/22/the-yield-statement-in-c/#comment-1158</guid>
		<description>Is the yield threadsafe?  What happens when you have 2 threads hitting the same static method?  Is the state maintained for each separate thread?</description>
		<content:encoded><![CDATA[<p>Is the yield threadsafe?  What happens when you have 2 threads hitting the same static method?  Is the state maintained for each separate thread?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Judah</title>
		<link>http://www.alteridem.net/2007/08/22/the-yield-statement-in-c/#comment-1157</link>
		<dc:creator>Judah</dc:creator>
		<pubDate>Thu, 23 Aug 2007 16:08:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.alteridem.net/2007/08/22/the-yield-statement-in-c/#comment-1157</guid>
		<description>One key point to add is that execution of yield methods are deferred: they aren't evaluated until you foreach over them. Thus, the following code, even though it looks like an infinite loop, is perfectly fine:

while(true)
{
   yield return 1;
}

It will simply keep yielding 1 until you stop foreach-ing over it.

Another point to consider is that since yield methods are delay-executed, any argument validation won't get applied until you foreach over it. For example,

IEnumerable GetEvens(IEnumerable input)
{
    if (input == null ) throw new ArgumentNullException();
   ...

   yield return ...;
}

Calling that method like this:
IEnumerable evens = GetEvents(null);

It won't result in an exception! Not until you foreach over evens.

For this reason, argument validation should be done first, then return the result of another method that does the actual yielding.</description>
		<content:encoded><![CDATA[<p>One key point to add is that execution of yield methods are deferred: they aren&#8217;t evaluated until you foreach over them. Thus, the following code, even though it looks like an infinite loop, is perfectly fine:</p>
<p>while(true)<br />
{<br />
   yield return 1;<br />
}</p>
<p>It will simply keep yielding 1 until you stop foreach-ing over it.</p>
<p>Another point to consider is that since yield methods are delay-executed, any argument validation won&#8217;t get applied until you foreach over it. For example,</p>
<p>IEnumerable GetEvens(IEnumerable input)<br />
{<br />
    if (input == null ) throw new ArgumentNullException();<br />
   &#8230;</p>
<p>   yield return &#8230;;<br />
}</p>
<p>Calling that method like this:<br />
IEnumerable evens = GetEvents(null);</p>
<p>It won&#8217;t result in an exception! Not until you foreach over evens.</p>
<p>For this reason, argument validation should be done first, then return the result of another method that does the actual yielding.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
