这部分代码是如何工作的

发布于 2024-09-11 04:13:57 字数 1569 浏览 1 评论 0原文

UPD。 你好, 我知道下面的代码是如何工作的。我知道cross和circle分别指向Cross()和Circle()方法。但我仍然对这部分代码感到困惑。你能为我解释一下吗?

public GameMoves()
            {
                cross = Cross();
                circle = Circle();
            }

所有代码

 static void Main(string[] args)
            {
                GameMoves game = new GameMoves();
                IEnumerator enumerator = game.Cross();
                while (enumerator.MoveNext())
                {
                    enumerator = (IEnumerator)enumerator.Current;
                }
            }
        }

        public class GameMoves
        {
            private IEnumerator cross;
            private IEnumerator circle;

            public GameMoves()
            {
                cross = Cross();
                circle = Circle();
            }

            private int move = 0;
            public IEnumerator Cross()
            {
                while (true)
                {
                    Console.WriteLine("X, step {0}", move);
                    move++;
                    if (move > 9)
                        yield break;
                    yield return circle;
                }
            }

            public IEnumerator Circle()
            {
                while (true)
                {
                    Console.WriteLine("O, step {0}", move);
                    move++;
                    if (move > 9)
                        yield break;
                    yield return cross;
                }
            }
        }

UPD.
Hello,
I know how code below is working. I know that cross, and circle are pointing to Cross(), and Circle() method. But I am still filling little confuse with this part of code. Can you explain it for me?

public GameMoves()
            {
                cross = Cross();
                circle = Circle();
            }

All code

 static void Main(string[] args)
            {
                GameMoves game = new GameMoves();
                IEnumerator enumerator = game.Cross();
                while (enumerator.MoveNext())
                {
                    enumerator = (IEnumerator)enumerator.Current;
                }
            }
        }

        public class GameMoves
        {
            private IEnumerator cross;
            private IEnumerator circle;

            public GameMoves()
            {
                cross = Cross();
                circle = Circle();
            }

            private int move = 0;
            public IEnumerator Cross()
            {
                while (true)
                {
                    Console.WriteLine("X, step {0}", move);
                    move++;
                    if (move > 9)
                        yield break;
                    yield return circle;
                }
            }

            public IEnumerator Circle()
            {
                while (true)
                {
                    Console.WriteLine("O, step {0}", move);
                    move++;
                    if (move > 9)
                        yield break;
                    yield return cross;
                }
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心小果奶 2024-09-18 04:13:57

CrossCircle 都是生成器。它们通过 IEnumerable 返回一个对象序列。也就是说,您可以编写:

foreach (var cross in Cross())  // works with Circle, also
{
    // ...
}

在每次循环迭代中,下一个要返回的元素在 CrossCircle 方法内生成。这些方法不会一次全部执行,而是每次到达 yield return 语句时,程序都会在调用代码(foreach 循环)中继续执行,并且仅当需要下一个项目时,才会恢复生成器内的代码。


PS:在我的互联网连接中断几个小时之前,我还想评论一个奇怪的事实,即您的生成器不断通过yield return返回对自身的引用。老实说,这对我来说确实没有意义。我从未见过这种代码,我想知道它是否真的有用?

Both Cross and Circle are generators. They return a sequence of objects by means of an IEnumerable. That is, you could write:

foreach (var cross in Cross())  // works with Circle, also
{
    // ...
}

And on every loop iteration, the next element to be returned is generated inside the Cross or Circle method. Those methods don't execute all at a time, instead each time a yield return statement is reached, program execution will continue in the calling code (the foreach loop), and the code inside the generator is only resumed when the next item is needed.


P.S.: Before my internet connection broke down for some hours, I had also wanted to comment on the strange fact that your generators keeps returning references to themselves via yield return. That doesn't really make sense to me, to be honest; I've never seen that kind of code and I wonder if it actually does something useful?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文