Yield return in C # And Coroutine in Unity (Coroutine) (upper ),
Yield return in C #
C # The syntax has a special keyword yield. What is yield used?
Let's take a look at the professional explanation:
Yield is used in the iterator block to provide values to enumeration objects or send an iteration end signal. It is in the form of one of the following:
Yield return <expression>;
Yield break
Take the following example:
1 1 public class CustomCollection: IEnumerable {2 2 3 3 public static void Main (string [] args) 4 4 {5 5 CustomCollection cc = new CustomCollection (); 6 6 7 7 foreach (String word in cc) {8 8 Console. writeLine ("word:" + word); 9 9} 10 10} 11 11 12 12 public IEnumerator GetEnumerator () {13 14 14 14 yield return "Hello "; 15 15 yield return "Boys"; 16 16 yield return "And"; 17 17 yield return "Girls"; 18 18 // return new HelloBoyGirls (); 19 19 20 20} 21 21} 22 22 23 23 // public class HelloBoyGirls: IEnumerator {24 // private int cusor =-1; 25 25 // private String [] words = {"Hello", "Boys", "And", "Girls "}; 26 26 // 27 27 // public bool MoveNext () 28 28 // {29 29 // cusor ++; 30 30 30 // return cusor <words. length; 31 31 //} 32 32 // 33 33 // public void Reset () 34 34 // {35 35 // cusor = 0; 36 36 //} 37 37 // 38 38 // public object Current {39 39 // get {40 40 // return words [cusor]; 41 41 //} 42 42 //} 43 43 //}View Code
The preceding example implements a custom iterator. To implement a data set that can be iterated (you can use foreach), you must implement the GetEmumerator () method and return the object instance that implements IEmumerator.
There are two methods to accomplish this. One is to use the code commented out above, and the other is to use yield return. yield return must be used with IEmumerator. In an external foreach loop, it executes the GetEmumerator () method. When yield return is encountered, the following two tasks are performed:
1. Record the currently executed Code Location
2. return the control right of the Code to the outside. The value after yield return is used as the current value of the iteration.
When the next loop is executed, the Code continues to be executed after the recorded code.
Simply put, yield return is a super simplified version of IEmumerator, isn't it easy?
Then again, how does yield return determine the end Of the loop? When will the code after yield return be executed?
Modify the example above. Do not use foreach for convenience. Use the while loop to control it by yourself:
1 public class CustomCollection: IEnumerable {2 3 public static void Main (string [] args) 4 {5 CustomCollection cc = new CustomCollection (); 6 7 IEnumerator enumerator = cc. getEnumerator (); 8 while (true) {9 bool canMoveNext = enumerator. moveNext (); 10 Console. writeLine ("canMoveNext:" + canMoveNext); 11 if (! CanMoveNext) 12 break; 13 Object obj = enumerator. current; 14 Console. writeLine ("current obj:" + obj); 15} 16 // foreach (String word in cc) {17 // Console. writeLine ("word:" + word); 18 //} 19 Console. writeLine ("Main End. "); 20 21} 22 23 public IEnumerator GetEnumerator () {24 25 yield return" Hello "; 26 yield return" Boys "; 27 yield return" And "; 28 yield return "Girls"; 29 30 Console. writeLine ("After all yield returns. "); 31 // return new HelloBoyGirls (); 32 33} 34}View Code
Run the code and the result is:
canMoveNext:True
current obj:Hello
canMoveNext:True
current obj:Boys
canMoveNext:True
current obj:And
canMoveNext:True
current obj:Girls
After all yield returns.
canMoveNext:False
Main End.
Note: In GetEmumerator (), only the yield return statement is available. The value of MoveNext () for external calls is true, and current is the object after yield return.
In addition to yield return, yield break; yield break is used to stop the loop and MoveNext () is false,Statements after yield break will not be executed!
If you are interested in shoes, you can write an example by yourself.