Implement an Enumerable Type Using a Custom Iterator - CSharp Collection

CSharp examples for Collection:Iterator

Description

Implement an Enumerable Type Using a Custom Iterator

Demo Code


using System;//from   w  w  w .j  a va 2 s  .c o  m
using System.Collections;

    public class TeamMember
    {
        public string Name;
        public string Title;

        public TeamMember(string name, string title)
        {
            Name = name;
            Title = title;
        }
        public override string ToString()
        {
            return string.Format("{0} ({1})", Name, Title);
        }
    }
    public class Team : IEnumerable
    {
        private class TeamMemberEnumerator : IEnumerator
        {
            private Team sourceTeam;
            private bool teamInvalid = false;
            private int currentMember = -1;
            internal TeamMemberEnumerator(Team team)
            {
                this.sourceTeam = team;

                sourceTeam.TeamChange += new TeamChangedEventHandler(this.TeamChange);
            }
            public object Current
            {
                get
                {
                    if (currentMember == -1 || currentMember > (sourceTeam.teamMembers.Count - 1))
                    {
                        throw new InvalidOperationException();
                    }
                    return sourceTeam.teamMembers[currentMember];
                }
            }
            public bool MoveNext()
            {
                if (teamInvalid)
                {
                    throw new InvalidOperationException("Team modified");
                }

                currentMember++;

                if (currentMember > (sourceTeam.teamMembers.Count - 1))
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            public void Reset()
            {
                if (teamInvalid)
                {
                    throw new InvalidOperationException("Team modified");
                }

                currentMember = -1;
            }

            internal void TeamChange(Team t, EventArgs e)
            {
                // Signal that the underlying Team is now invalid.
                teamInvalid = true;
            }
        }
        public delegate void TeamChangedEventHandler(Team t, EventArgs e);
        private ArrayList teamMembers;
        public event TeamChangedEventHandler TeamChange;
        public Team()
        {
            teamMembers = new ArrayList();
        }
        public IEnumerator GetEnumerator()
        {
            return new TeamMemberEnumerator(this);
        }
        public void AddMember(TeamMember member)
        {
            teamMembers.Add(member);
            if (TeamChange != null)
            {
                TeamChange(this, null);
            }
        }
    }

class MainClass
    {
        public static void Main()
        {
            Team team = new Team();
            team.AddMember(new TeamMember("C", "Coder"));
            team.AddMember(new TeamMember("N", "Programmer"));
            team.AddMember(new TeamMember("N", "Tester"));

            Console.WriteLine("Enumerate with foreach loop:");
            foreach (TeamMember member in team)
            {
                Console.WriteLine(member.ToString());
            }

            Console.WriteLine("Enumerate with while loop:");
            IEnumerator e = team.GetEnumerator();
            while (e.MoveNext())
            {
                Console.WriteLine(e.Current);
            }

            Console.WriteLine("Modify while enumerating:");
            foreach (TeamMember member in team)
            {
                Console.WriteLine(member.ToString());
                team.AddMember(new TeamMember("Stumpy", "Lion Tamer"));
            }
        }
    }

Result


Related Tutorials