Proper use of 'yield return'
I tend to use 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.
Populating a temporary list is like downloading the whole video, whereas using yield
is like streaming that video.
As a conceptual example for understanding when you ought to use yield
, let's say the method ConsumeLoop()
processes the items returned/yielded by ProduceList()
:
void ConsumeLoop() {
foreach (Consumable item in ProduceList()) // might have to wait here
item.Consume();
}
IEnumerable<Consumable> ProduceList() {
while (KeepProducing())
yield return ProduceExpensiveConsumable(); // expensive
}
Without yield
, the call to ProduceList()
might take a long time because you have to complete the list before returning:
//pseudo-assembly
Produce consumable[0] // expensive operation, e.g. disk I/O
Produce consumable[1] // waiting...
Produce consumable[2] // waiting...
Produce consumable[3] // completed the consumable list
Consume consumable[0] // start consuming
Consume consumable[1]
Consume consumable[2]
Consume consumable[3]
Using yield
, it becomes rearranged, sort of interleaved:
//pseudo-assembly
Produce consumable[0]
Consume consumable[0] // immediately yield & Consume
Produce consumable[1] // ConsumeLoop iterates, requesting next item
Consume consumable[1] // consume next
Produce consumable[2]
Consume consumable[2] // consume next
Produce consumable[3]
Consume consumable[3] // consume next
And lastly, as many before have already suggested, you should use Version 2 because you already have the completed list anyway.