In the method of IEnumerator return type
Yield return to keep the enumeration state (you can take a few demo after the Ilspy to see the source, you know)
When the editor encounters the above situation, all calls that generate classes to hold yield return are branched (state machine Branch), and the next time the Ienumerator.movenext starts with the last state (e.g., iteration to number)
Yield break is to end all of the empty state information (not retained), ending the IEnumerator iteration
For a specific look at MSDN Introduction:
Http://msdn.microsoft.com/zh-cn/library/9k7k7cf0.aspx
Demo Source:
Using System;
Using System.Collections;
Using System.Threading; namespace Yieldkeywordtestingproject {///<summary>///jave.lin///20114-02-12///</SUMMARY&G
T
Class Program {static bool delay = TRUE;
static void Main (string[] args) {var result = GetResult (); Console.WriteLine ("The WriteLine:" + result.
Current); while (result. MoveNext ()) {Console.WriteLine ("outside WriteLine: {0}", result.
Current);
} Console.WriteLine ("Yield return over!!!");
Console.ReadLine ();
Static IEnumerator GetResult () {//var arr = array.createinstance (typeof (Program), 1);
var arr = new int[] {1, 2, 3, 45, 23, 23, 4, 3213};
foreach (var item in arr) {yield return item;
Console.WriteLine (item); The key here, if the output is delay true,is the same as delay false;//So you have a deeper understanding of yield return; if (delay) thread.sleep (100);
}
}
}
}
Visual Studio creates a C # console project;
Operation Effect:
Note that after I change the delay to True
The picture is not complete, you can right-click the picture, copy the image address, and then open a new child page, copy the past look;
or some browser directly has: In the new tab open the picture, such as the menu of options;