-----------------------------------------------------------
New Message on BDOTNET
-----------------------------------------------------------
From: dnyanesh
Message 3 in Discussion
Hi All, Ques: Where would you use yield return and yield break ? Ans:
Iterator Implementation
The compiler-generated nested class maintains the iteration state. When the
iterator is first called in a foreach loop (or in direct iteration code), the
compiler-generated code for GetEnumerator creates a new iterator object (an
instance of the nested class) with a reset state. Every time the foreach loops
and calls the iterator's MoveNext method, it begins execution where the
previous yield return statement left off. As long as the foreach loop executes,
the iterator maintains its state. However, the iterator object (and its state)
does not persist across foreach loops. Consequently, it is safe to call foreach
again because you will get a new iterator object to start the new iteration.
This is why IEnumerable<ItemType> does not define a Reset method.
But how is the nested iterator class implemented and how does it manage its
state? The compiler transforms a standard method into a method that is designed
to be called multiple times and that uses a simple state machine to resume
execution after the previous yield statement. All you have to do is indicate
what and when to yield to the compiler using the yield return statement. The
compiler is even smart enough to concatenate multiple yield return statements
in the order they appear:
public class CityCollection : IEnumerable<string>
{
public IEnumerator<string> GetEnumerator()
{
yield return "New York";
yield return "Paris";
yield return "London";
}
}
The keyword yield break
You might wish to enumerate a subset of an enumerable. In this case, the
keyword yield break is the right way to tell the client that it should stop
looping.
This program outputs:
Michel
Christine
As a consequence, the code written after a yield break instruction is not
reachable. The C# compiler emits a warning when meeting such unreachable code.
Syntactic constraints on yield return and yield break keywords
yield break and yield return keywords can be used only inside the body of a
method, the body of a property accessor or the body of an operator.
Whatever the kind of method that use yield break or yield return keywords, it
must return one the following interfaces
System.Collections.Generic.IEnumerable<T> , System.Collections.IEnumerable ,
System.Collections.Generic.IEnumerator<T> or System.Collections.IEnumerator .
yield break and yield return keywords cant be used in the body of an
anonymous method.
yield break and yield return keywords cant be used in a finally block.
yield break and yield return keywords cant be used in a try block that has
at least a catch block.
yield break and yield return keywords cant be used in a method that has ref
or out arguments. More globally, a method that contains at least one of these
keywords should not return any other information than the yielded item.
Regards,
Dnyaneshwar Parkhe
-----------------------------------------------------------
To stop getting this e-mail, or change how often it arrives, go to your E-mail
Settings.
http://groups.msn.com/BDOTNET/_emailsettings.msnw
Need help? If you've forgotten your password, please go to Passport Member
Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help
For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact
If you do not want to receive future e-mail from this MSN group, or if you
received this message by mistake, please click the "Remove" link below. On the
pre-addressed e-mail message that opens, simply click "Send". Your e-mail
address will be deleted from this group's mailing list.
mailto:[EMAIL PROTECTED]