In my everlasting quest to suck less I keep bumping into the "yield" statement.
I have tried to wrap my head around it a few times now, but I'm stumped every time with the same error.
It goes a little something like this:
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 last got stuck:
foreach (XElement header in headersXml.Root.Elements()){
yield return (ParseHeader(header));
}
What am I doing wrong? Can't I use the yield in an iterator? Then what's the point? In the case 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, not?
--Edit-- Thanks for all the quick answers. Answer given to first answer. I know this question isn't all that new and the same resouces keep popping up. It turned out I was thinking I could return List as a return type, but off course because List isn't lazy, it cannot be used. Changing my return type to IEnumerable solved the problem :D
Now a somewhat related question (not worth opening a new thread): is it worth giving IENumerable as a return type if I'm sure that 99% of the cases I'm going to go .ToList() anyway? What will performance do for me there?