tags:

views:

121

answers:

1

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;
                }
            }
        }
+1  A: 

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?

stakx
@stakx: This code is example from book. It is illustrate how operator yield is working. But for some reason It makes me confuse. That is why I put my question.
Sergii
If that's from a textbook, and the other code examples in it are like that, you should consider getting rid of the book. It's not difficult coming up with better examples of `yield return` and `yield break`. A very basic example is this: **(1)** Define an iterator: `IEnumerable<int> Range(int from, int to) { for (int i = from; i < to; i++) yield return i; }` -- **(2)** Example usage of the iterator: `foreach (int i in Range(1, 10)) { Console.WriteLine(i); }`
stakx