patterncsharpCritical
Proper use of 'yield return'
Viewed 0 times
properyieldusereturn
Problem
The yield keyword is one of those keywords in C# that continues to mystify me, and I've never been confident that I'm using it correctly.
Of the following two pieces of code, which is the preferred and why?
Version 1: Using yield return
Version 2: Return the list
Of the following two pieces of code, which is the preferred and why?
Version 1: Using yield return
public static IEnumerable GetAllProducts()
{
using (AdventureWorksEntities db = new AdventureWorksEntities())
{
var products = from product in db.Product
select product;
foreach (Product product in products)
{
yield return product;
}
}
}Version 2: Return the list
public static IEnumerable GetAllProducts()
{
using (AdventureWorksEntities db = new AdventureWorksEntities())
{
var products = from product in db.Product
select product;
return products.ToList();
}
}Solution
I tend to use
Using your Version 2, you must have the complete list before returning.
By using
Among other things, this helps spread the computational cost of complex calculations over a larger time-frame. For example, if the list is hooked up to a GUI and the user never goes to the last page, you never calculate the final items in the list.
Another case where
In your particular example, you have the full list of products, so I'd use Version 2.
yield return when I calculate the next item in the list (or even the next group of items).Using your Version 2, you must have the complete list before returning.
By using
yield return, you really only need to have the next item before returning.Among other things, this helps spread the computational cost of complex calculations over a larger time-frame. For example, if the list is hooked up to a GUI and the user never goes to the last page, you never calculate the final items in the list.
Another case where
yield return is preferable is if the IEnumerable represents an infinite set. Consider the list of Prime Numbers, or an infinite list of random numbers. You can never return the full IEnumerable at once, so you use yield return to return the list incrementally.In your particular example, you have the full list of products, so I'd use Version 2.
Context
Stack Overflow Q#410026, score: 873
Revisions (0)
No revisions yet.