Monday, May 9, 2011

yield vs return in C#

The yield keyword signals to the compiler that the method in which it appears is an iterator block.

  • yield can be used to return partially.
  • yield maintains the state of the object
Simple example

void Main()
foreach(int i in GetDivideByTwo())

static System.Collections.Generic.IEnumerable GetDivideByTwo()
for (int i = 0; i < 100; i++)
yield return i/2;

Here is couple of good links

No comments:

Post a Comment