Yield C#

Used with Enumerator and Enumerable.
Instead of creating a List and then passing ...
each time Foreach call for WholeList...it gets one item from yield.

When you use the "yield return" keyphrase, .NET is wiring up a whole bunch
of plumbing code for you, but for now you can pretend it’s magic.
When you start to loop in the calling code (not listed here), this function actually
gets called over and over again, but each time it resumes execution where it left off.


Code Samples: Every time you call GetInt() you will receive a new incremented integer.
public static IEnumerable<int> GetInt(){ 
  for (int i = 0; i < 5; i++) 
      yield return i;
}

the same technique is used in http://www.foodtwitter.com app

Comments

Post a Comment