yield return versus return select

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?

yield return and return

I often find myself writing sth. like this: if (condition) { yield return whatever; yield break; } I find it quite verbose to have to use two yield statements for the standard paradigm return one va

yield return weird behavior

I have the following code: virtual public IEnumerable<string> GetSelectedIds(){ if (_kids == null) yield return null; foreach (var current in _kids.Nodes) yield return current; } This piece of

GetEnumerator: return or yield return

My class implements IEnumerable. And these two ways to code GetEnumerator method can be compiled: public IEnumerator GetEnumerator() { yield return Database[id].GetEnumerator(); } and public IEnumera

Yield Return with Null

Is there any way to optionally return a null with a return yield driven iterator? I would like to return a null in some cases and I don’t think this is particular to IEnumerable of type string. Same

Length of Yield Return

Is there a way to get the number of yield returns from within a function without keeping a counter variable? For instance? IEnumerable<someobject> function { for loop yield return something int

What concrete type does ‘yield return’ return?

What is the concrete type for this IEnumerable<string>? private IEnumerable<string> GetIEnumerable() { yield return a; yield return a; yield return a; }

Yield return brainfreeze

Say I have a method like this: IEnumerable<record> GetSomeRecords() { while(…) { yield return aRecord } } Now, lets say I have a caller that also returns an enumerable of the same type, somet

yield return usage

Approach 1: class myClass { List<SomeType> _list; IENumerator<SomeType> GetEnumerator() { foreach(SomeType t in _list) yield return t; } } myClass m = new myClass(); List<SomeType> l

Accessing yield return collection

Is there any way to access the IEnumerable<T> collection being build up by yield return in a loop from within the method building the IEnumerable itself? Silly example: Random random = new Rando

Yield return and dynamic

Is possible to invoke a IEnumerable/yield return method when using dynamic? I’m asking this because I’m getting the error below when I call the Test(States1.GetNames()) method. Error: Additional

Answers

The yield return technique causes the C# compiler to generate an enumerator class “behind the scenes”, while the Select call 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 the Select case, for the delegate.

For what it’s worth, wrapping a lambda around DoSomething is sort of pointless as well; just pass a delegate for it directly.

Select only allows you to return one object for each item in your “items” collection. Using an additional .Where(x => DoIReallyWantThis(x)) allows you to weed out unwanted items, but still only allows you to return one object per item. If you want potentially more than one object per item, you can use .SelectMany but it is easy to wind up with a single long line that is less than easy to read.

“yield return” has the potential to make your code more readable if you are looking through a complex data structure and picking out bits of information here and there. The best example of this that I have seen was where there were around a dozen separate conditions which would result in a returned object, and in each case the returned object was constructed differently.

In the slow-moving corporate world where I currently spend more time than I’d wish, yield return has the enormous advantage that it doesn’t need that brand new .NET 3.5 Framework that won’t be installed for at least another 2 years.