Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 6 > BOOK: Professional C# 6 and .NET Core 1.0
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional C# 6 and .NET Core 1.0
This is the forum to discuss the Wrox book Professional C# 6 and .NET Core 1.0 by Christian Nagel; ISBN: 978-1-119-09660-3
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional C# 6 and .NET Core 1.0 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old June 4th, 2018, 09:12 AM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2018
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.
Reply With Quote
  #2 (permalink)  
Old June 4th, 2018, 02:06 PM
Wrox Author
Points: 1,160, Level: 13
Points: 1,160, Level: 13 Points: 1,160, Level: 13 Points: 1,160, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2010
Location: Austria
Posts: 174
Thanks: 3
Thanked 53 Times in 53 Posts
Default

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.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel

Last edited by ChristianNagel; June 5th, 2018 at 03:58 AM.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 10 CardLibrary.cs dravrilev BOOK: Beginning C# 6.0 Programming with Visual Studio 2015 0 November 14th, 2017 12:17 PM
Chapter 11 page 290 Decks.cs dampyr BOOK: Beginning Visual C# 2010 3 November 20th, 2011 05:27 AM
Chapter 15:Changes to UIMap.Designer.cs David Lunn BOOK: Professional Application Lifecycle Management with Visual Studio 2010 1 June 21st, 2011 02:02 AM
Chapter 5 UserController.cs P.119-129 ralphbethke BOOK: ASP.NET MVC Website Programming Problem Design Solution ISBN: 9780470410950 34 February 19th, 2011 09:40 PM
Chapter 7 Object reference error in master.cs mikeal_a BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 4 September 5th, 2008 07:19 AM



All times are GMT -4. The time now is 11:50 AM.


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