Understanding yield keyword in C#

Yield one of the most useful keywords in C# but it’s not used in many cases due to that most of us don’t know about this keyword purpose or how to use it

yield keyword effectively creates a lazy enumeration over collection items that can be much more efficient.

For example, yield return is a .NET sugar to return an IEnumerable with the only needed items.

Code without yield:

class MyItem{
public MyItem() { }
static public IEnumerable CreateListOfItems(){
return new List {
new MyItem(),
new MyItem(),
new MyItem() };
}
}

Same code using yield:

class MyItem
{
public MyItem() { }
static public IEnumerable CreateListOfItems()
{
yield return new MyItem();
yield return new MyItem();
yield return new MyItem();
}
}

The advantage of using yield is that if the function consuming your data simply needs the first item of the collection, the rest of the items won’t be created so it’s more efficient.
The yield operator allows the creation of items as it is demanded. That’s a good reason to use it.

For example, if your foreach loop iterates over just the first 3 items of 2 million items then that’s all yield returns, and you didn’t build up a collection of 2 million items internally first. Likewise you will want to use yield with IEnumerable return values in your own programming scenarios to achieve the same efficiencies.

Another example: inefficient code that call method that returns list of million item but we actually need the first 3 items:

// Method returns all  million items before anything can loop over them. 
List<object> GetAllItems() {
List<object> millionCustomers = GetMillionCustomerFromDatabase();
return millionCustomers;
}
// Caller code sample:
int num = 0;
foreach(var item in GetAllItems()) {
num++;
if (num == 3)
break;
}
// Note: One million items returned, but only 5 used.

To solve this issue we will simply using yield keyword:

// Yields items one at a time as the caller's foreach loop requests them
IEnumerable<object> IterateOverItems() {
for (int i; i < database.Customers.Count(); ++i)
yield return database.Customers[i];
}
// Caller code sample:
int num = 0;
foreach(var itm in IterateOverItems()) {
num++;
if (num == 3)
break;
}
// Note: Only 3 items were yielded and used out of the million.
Show your support

Clapping shows how much you appreciated Ahmed Marzouk’s story.