//yield return 返回類型必須爲 IEnumerable、IEnumerable<T>、IEnumerator 或 IEnumerator<T>。spa
static IEnumerator<int> yieldTest() //yield return 返回IEnumerator
{
yield return 1;
yield return 4;
if (true)//若是爲True 輸出 1,4;//若是是False 輸出 1,4,3,2
{
yield break;
}
yield return 3;
yield return 2;
}
static void Main(string[] args)
{
var list = yieldTest();
while (list.MoveNext())
{
Console.WriteLine(list.Current);
}
Console.Read();
}get
static IEnumerable<int> yieldTest2()//yield return 返回IEnumerable
{
yield return 1;
yield return 4;
if (!false)
{
yield break;
}
yield return 3;
yield return 2;
}
static void Main(string[] args)
{
var list = yieldTest2();
foreach(var item in list)
{
Console.WriteLine(item);
}
Console.Read();
}string