When can I use the yield keyword to define iterators?
- The return type of the iterator must be IEnumerable, ienumerable<t>, IEnumerator, or ienumerator<t>
- An iterator's entry parameter cannot include a ref or out type parameter
First, we define a simple traversal.
Static voidMain (string[] args) {List<Person> persons =NewList<person>(); Persons. ADD (Newperson {saysome ="Hello world,i am Sheldon" }); Persons. ADD (Newperson {saysome ="Hello world,i am Penny" }); Persons. ADD (Newperson {saysome ="Hello world,i am Amy" }); foreach(varIteminchpersons) {Console.WriteLine (item. Saysome); } }
The result of its output:
Next, we use yield to achieve the same effect.
Static voidMain (string[] args) { foreach(varIteminchGetEnumerator ()) {Console.WriteLine (item. Saysome); } } Public StaticIenumerable<person>GetEnumerator () {yield return Newperson {saysome ="Hello world,i am Sheldon" }; yield return Newperson {saysome ="Hello world,i am Penny" }; yield return Newperson {saysome ="Hello world,i am Amy" }; }
Although we got the same result, how did yield actually do it?
(Again the book, and see MSDN, finally got the answer)
When you use the yield statement, it automatically generates an enumerator instead of just a list of included items.
—————————————————— above about yield describes the approximate ———————————————————————
—————————————————— The following mainly describes how foreach iterates ———————————————————————
This enumerator accesses the enumerator through the foreach call, which accesses each item in turn, thus iterating through a large amount of data without having to write all the data into memory at once.
On the enumerator, I looked at the source code under the System.collection.generic space.
To know how a foreach works through an enumerator.
Let's declare a simple enumerator based on the list set above
(This enumerator, just to explain the problem briefly)
Public classgamemoves {PrivateIEnumerator Cross; PrivateIEnumerator Circle; Publicgamemoves () { Cross=Cross (); Circle=Circle (); } Private intmove =0; Const intMaxmoves =9; PublicIEnumerator Cross () { while(true) {Console.WriteLine ("Cross , move {0}", move); if(++move >=maxmoves) { yield Break; } yield returnCircle; } } PublicIEnumerator Circle () { while(true) {Console.WriteLine ("circle,move{0}", move); if(++move>=maxmoves) { yield Break; } yield returnCross ; } } }
View Code
Rewrite the Main method
var New gamemoves (); // sets the enumerator to be set by the game. Cross () returns the enumerator type IEnumerator enumerator = game. Cross (); // The cross () method is called when MoveNext () is called for the first time, and the Cross () method returns another enumerator using yield while (Enumerator. MoveNext ()) { // The value returned can be accessed with the current property and set to the enumerator variable for the next loop as IEnumerator; }
View Code
From the example above we can see that using the while to change to explain the internal execution of foreach.
Use the iterator method with a foreach statement or a LINQ query.
Each iteration of the Foreach loop invokes an iterator method. When the iterator method runs to the yield return statement, it returns an expression and retains the current position in the code. The next time the iterator function is called, execution will start again from that location.
(partly from MSDN)
C # Basics yield vs. foreach