public class GameMoves
{
private IEnumerator _cross;
private IEnumerator _cirle;
private int _move = 0;
const int Maxcount = 9;
public GameMoves()
{
_cross = Cross();
_cirle = Circle();
}
public IEnumerator Cross()
{
while (true)
{
Console.WriteLine($"Cross move:{_cross}");
if(++_move > Maxcount)
{
yield break;
}
yield return _cirle;
}
}
public IEnumerator Circle()
{
while (true)
{
Console.WriteLine($"Circle move:{_cirle}");
if (++_move > Maxcount)
{
yield break;
}
yield return _cross;
}
}
}
yield break终止迭代
yield return可以返回一个数据,也可以返回一个迭代器继续通过返回的迭代器进行迭代遍历
yield return返回的值可以通过Current属性访问