Which are the advantages/drawbacks of both approaches?
return items.Select(item => DoSomething(item));
versus
foreach(var item in items)
{
yield return DoSomething(item);
}
EDIT As they are MSIL roughly equivalent, which one you find more readable?
The
yield returntechnique causes the C# compiler to generate an enumerator class “behind the scenes”, while theSelectcall uses a standard enumerator class parameterized with a delegate. In practice, there shouldn’t be a whole lot of difference between the two, other than possibly an extra call frame in theSelectcase, for the delegate.For what it’s worth, wrapping a lambda around
DoSomethingis sort of pointless as well; just pass a delegate for it directly.