Rob Garrett - Blogs

Welcome to Rob Garrett - Blogs Sign in | Join | Help
in Search
Google

Software/Technology Discussion

Software and Technology Tid-bits

C# 2.0 and the 'yield' keyword

Another nice addition to C# 2.0 is the yield keyword. Yield enables iterator blocks to provide values to an enumerated result, see the following example.


What does this mean in English? Essentially, iterator blocks can implicitly create an array of results, which supports IEnumerable, without having to declare an array data type and populating it.

The following line will signal the end of the iteration.

Share this post: Email it! | bookmark it! | digg it! | reddit!
Published Tuesday, September 13, 2005 10:29 AM by Rob Garrett

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Sahil Malik said:

And (VB.NET) that (VB.NET) other (VB.NET) language (VB.NET) we (VB.NET) won'(VB.NET) t (VB.NET) mention, (VB.NET) doesn't support yield. Now isnt' that crappy?(VB.NET)
September 13, 2005 12:00 PM
 

Nathan said:

Anyone think C# is getting a little too overloaded with keywords? A little syntactic sugar is nice, but...
September 14, 2005 8:41 AM
 

Rob Garrett said:

If adding a new keyword prevents me from having to write a few more lines of code, then I'm all for it. Check out http://msdn2.microsoft.com/en-us/library/x53a06bb for a list of keywords in c# 2.

Now, if only VB.NET was as good as C#.....
September 14, 2005 9:05 AM
 

cheap international calls said:

Why would you even want to use VB anyway? :-)
October 12, 2005 6:22 AM
 

Trev said:

Use the right tool for the job I say.... sure, custom iterators are more work to do in VB.Net, but just try interoperating with Excel using C#... lack of optional parameters and no late binding make it very difficult. (Note... I'm not advocating late binding... it's just very handy when you're dealing with different versions of the same library). Choose your tools wisely and without prejudice.
May 3, 2007 6:17 PM
 

Lars Corneliussen said:

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/
June 10, 2008 5:44 PM

Leave a Comment

(required) 
(optional)
(required) 
Submit

Blurb


Head Shot
Rob Garrett is a British Expat living in Maryland USA. Rob is a trained software engineer and experienced in Windows .NET development.

Rob enjoys listening to Rock music, posting to blogs, driving in the country with the sunroof open, beer (not in conjunction with country driving) and spending time with his family.

This Blog

Syndication

Powered by Community Server, by Telligent Systems