[Mono-list] To yield or to not

Raja R Harinath rharinath at novell.com
Fri Sep 23 10:30:10 EDT 2005


Hi,

"Gaurav Vaish" <gaurav.vaish at gmail.com> writes:

>     Finally I somewhat understood how yield works.. probably, the correctly.
>
>     And noting how it works (acquiring and releasing locks), is it a
> good idea to use yield? Or is it better to stick with plain old
> IEnumerable and IEnumerator, may be with a pinch of generics...

Nah.  yield is cool.

>     I am yet to figure out "why was this feature introduced"? I
> mean... what advantage does it give besides having, probably, lesser
> code to write.

What else do you need? ;-)

In fact, writing correct iterators is not very straightforward.  So, any
help we get from the compiler is welcome.  We only deal with generating
the elements in order; the compiler takes care of the rest of the
semantics: like appropriate exceptions when Current is called at the
wrong time, handling calls to MoveNext() after it has returned false,
etc.

Also, the co-routine style is probably more natural for generating lists
of things, rather than the inside-out MoveNext style of writing
iterators.  The compiler takes care of taking a nice almost sane-looking
(co-)routine and turns it into a messy inside-out MoveNext.

- Hari


More information about the Mono-list mailing list