Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some help understanding "yield"

Tags:

iterator

yield

c#

In my everlasting quest to suck less I'm trying to understand the "yield" statement, but I keep encountering the same error.

The body of [someMethod] cannot be an iterator block because 'System.Collections.Generic.List< AClass>' is not an iterator interface type.

This is the code where I got stuck:

foreach (XElement header in headersXml.Root.Elements()){     yield return (ParseHeader(header));                 } 

What am I doing wrong? Can't I use yield in an iterator? Then what's the point? In this example it said that List<ProductMixHeader> is not an iterator interface type. ProductMixHeader is a custom class, but I imagine List is an iterator interface type, no?

--Edit--
Thanks for all the quick answers.
I know this question isn't all that new and the same resources keep popping up.
It turned out I was thinking I could return List<AClass> as a return type, but since List<T> isn't lazy, it cannot. Changing my return type to IEnumerable<T> solved the problem :D

A somewhat related question (not worth opening a new thread): is it worth giving IEnumerable<T> as a return type if I'm sure that 99% of the cases I'm going to go .ToList() anyway? What will the performance implications be?

like image 200
Boris Callens Avatar asked Nov 25 '08 14:11

Boris Callens


People also ask

How do you interpret yield?

Yield on Stocks For example, if an investor realized a profit of $20 ($120 - $100) resulting from price rise, and also gained $2 from a dividend paid by the company. Therefore, the cost yield comes to ($20 + $2) / $100 = 0.22, or 22%. For example, the current yield comes to ($20 + $2) / $120 = 0.1833, or 18.33%.

What are the three types of yield?

The slope of the yield curve gives an idea of future interest rate changes and economic activity. There are three main types of yield curve shapes: normal (upward sloping curve), inverted (downward sloping curve) and flat.

Why is yield so important?

The yield is considered a marker for investor confidence in the markets, shining a light on whether investors feel they can make a higher return than the yield offered on a 10-year note by investing in stocks, ETFs, or other riskier securities.


2 Answers

A method using yield return must be declared as returning one of the following two interfaces:

IEnumerable<SomethingAppropriate> IEnumerator<SomethingApropriate> 

(thanks Jon and Marc for pointing out IEnumerator)

Example:

public IEnumerable<AClass> YourMethod() {     foreach (XElement header in headersXml.Root.Elements())     {         yield return (ParseHeader(header));                     } } 

yield is a lazy producer of data, only producing another item after the first has been retrieved, whereas returning a list will return everything in one go.

So there is a difference, and you need to declare the method correctly.

For more information, read Jon's answer here, which contains some very useful links.

like image 99
Lasse V. Karlsen Avatar answered Sep 30 '22 09:09

Lasse V. Karlsen


It's a tricky topic. In a nutshell, it's an easy way of implementing IEnumerable and its friends. The compiler builds you a state machine, transforming parameters and local variables into instance variables in a new class. Complicated stuff.

I have a few resources on this:

  • Chapter 6 of C# in Depth (free download from that page)
  • Iterators, iterator blocks and data pipelines (article)
  • Iterator block implementation details (article)
like image 25
Jon Skeet Avatar answered Sep 30 '22 10:09

Jon Skeet