p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional C# 6 and .NET Core 1.0 (http://p2p.wrox.com/forumdisplay.php?f=801)
-   -   Chapter 7: GameMoves.cs (http://p2p.wrox.com/showthread.php?t=99904)

oda356 June 4th, 2018 09:12 AM

Chapter 7: GameMoves.cs
 
Hello, I'm learning chapeter 7, which talks about the array, and emuerator. I can't understand how the GameMove.cs works. Could you help me?
Code:

   
public class GameMoves
    {
        private IEnumerator _cross;
        private IEnumerator _circle;
        public GameMoves()
        {
            _cross = Cross();
            _circle = Circle();
        }
        private int _move = 0;
        const int MaxMoves = 9;
        public IEnumerator Cross()
        {
            while (true)
            {
                WriteLine($"Cross, move {_move}");
                if (++_move >= MaxMoves)
                    yield break;
                yield return _circle;
            }
        }
        public IEnumerator Circle()
        {
            while (true)
            {
                WriteLine("Circle, move {0}", _move);
                if (++_move >= MaxMoves)
                    yield break;
                yield return _cross;
            }
        }
    }

Code:


            var game = new GameMoves();
            IEnumerator enumerator = game.Cross();
            while (enumerator.MoveNext())
            {
                enumerator = enumerator.Current as IEnumerator;
            }

In the initialization of the game object, the
Code:

_cross = Cross();
calls the Cross() function, which return the _cross IEnumerator, but it is not initialized at that time. What makes it work?

When the
Code:

enumerator.MoveNext()
is called, does the enumerator.Current change to the _circle IEnumerator? If so, when the
Code:

enumerator.MoveNext()
is called again what mechanism associate the _circle and Circle(), to make the Circle() function called then?

Thank you.

ChristianNagel June 4th, 2018 02:06 PM

The magic is the yield statement.

The yield statement creates an enumerator. Because of the yield statement, invoking the Cross method in the GameMoves constructor, the code you see implemented in this method will not be called - you can verify this setting breakpoints and running the application in the debugger. Instead, the yield statement creates a class implementing the interface IEnumerator. An object of this class is returned invoking the Cross method.

Code:

   
    public class GameMoves
    {
        private IEnumerator _cross;
        private IEnumerator _circle;
        public GameMoves()
        {
            _cross = Cross();
            _circle = Circle();
        }
        private int _move = 0;
        const int MaxMoves = 9;
        public IEnumerator Cross()
        {
            while (true)
            {
                WriteLine($"Cross, move {_move}");
                if (++_move >= MaxMoves)
                    yield break;
                yield return _circle;
            }
        }
        public IEnumerator Circle()
        {
            while (true)
            {
                WriteLine("Circle, move {0}", _move);
                if (++_move >= MaxMoves)
                    yield break;
                yield return _cross;
            }
        }
    }

Invoking the MoveNext method, you'll see the debugger stops at a breakpoint within the Cross method - at this time the _cross and _circle fields are already filled with the IEnumerator implementing objects.


Code:


            var game = new GameMoves();
            IEnumerator enumerator = game.Cross();
            while (enumerator.MoveNext())
            {
                enumerator = enumerator.Current as IEnumerator;
            }

I hope this helps.


For discussions and questions on the new book Professional C# 7 and .NET Core 2.0, please create an issue in https://github.com/ProfessionalCShar...ssionalCSharp7.

For Professional C# 6 questions, you can use GitHub as well: https://github.com/ProfessionalCShar...ssionalCSharp6.


All times are GMT -4. The time now is 07:53 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.