.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Yield keyword in c#

Posted By:Deco       Posted Date: October 25, 2010    Points: 50    Category: C#    URL: http://www.dotnetspark.com  

This article deals with the use of yield key word and it's implementation.

Some cool and hidden facts of C# is the yield keyword. Among us many of them does not know it's feature.
The yield keyword is used in an iterator block to provide a value to the enumerator object
or to signal the end of the iteration. When used the expression is evaluated and returned
as a value to the enumerator object. Note the expression has to be implicitebly convertible
to yield type of the iterator.

Here is an example

public static IEnumerable GetIntCollectionFromString(string InputString)
string[] StringArray = InputString.Split(' ');
int intVal;
foreach (string token in StringArray)
if (int.TryParse(token, out intVal))
yield return intVal;
yield break;
Here since we are using the yield keyword the function will return the collection of intVal
instead of the value of intVal. The statement will only return when the iteration of the loop is complete.

There are some limitation with the yield keywords.

1.Unsafe blocks are not allowed
2.Parameters to the method, operator, or accessor cannot be ref or out.
 Subscribe to Articles


Further Readings:

Author: Alex         Company URL: http://www.dotnetspark.com
Posted Date: October 26, 2010

Useful info.. Great Alwyn
Author: Dhivakaran         Company URL: http://www.dotnetspark.com
Posted Date: October 29, 2010

We can more information about yeild keywords using below URL



Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend