Example #1
0
        static void MyIterator()
        {
            #region student
            Students students = new Students();
            Student st = new Student();
            st.Name = "lili";

            students.Add(st);
            st = new Student();
            st.Name = "OK";

            students.Add(st);

            foreach (Student obj in students)
            {
                Console.WriteLine(obj.Name);
            }
            #endregion

            #region teacher
            Teachers ts = new Teachers();
            Teacher t1 = new Teacher(1001, "t1", 33);
            Teacher t2 = new Teacher(1002, "t2", 34);
            ts.Add(t1);
            ts.Add(t2);

            foreach (Teacher t in ts)
            {
                Console.WriteLine("{0}, {1}, {2};", t.Tid.ToString(), t.Name, t.Age.ToString());
            }

            // other methods for the foreach
            {
                IEnumerator enumerator = ts.GetEnumerator();
                Teacher t;

                while (enumerator.MoveNext())
                {
                    t = (Teacher)enumerator.Current;
                    Console.WriteLine("{0}, {1}, {2};", t.Tid.ToString(), t.Name, t.Age.ToString());
                }
            }

            #endregion

            #region doctor
            // using IEnumerable<T> interface
            Doctors ds = new Doctors();
            Doctor d1 = new Doctor("lili", 28);
            Doctor d2 = new Doctor("wang", 30);
            ds.Add(d1);
            ds.Add(d2);

            foreach (Doctor d in ds)
            {
                Console.WriteLine("{0}, {1};", d.Name, d.Age.ToString());
            }
            Console.WriteLine();

            {
                IEnumerator enumerator = ds.GetEnumerator();
                Doctor d;

                while (enumerator.MoveNext())
                {
                    d = (Doctor)enumerator.Current;
                    Console.WriteLine("{0}, {1};", d.Name, d.Age.ToString());
                }
            }

            Console.WriteLine();

            {
                IEnumerator<Doctor> enumerator = ds.GetEnumerator();
                Doctor d;

                while (enumerator.MoveNext())
                {
                    d = enumerator.Current;
                    Console.WriteLine("{0}, {1};", d.Name, d.Age.ToString());
                }
            }
            #endregion

            #region myStack

            MyStack<int> s = new MyStack<int>();
            for (int i = 0; i < 10; i++)
            {
                s.Push(i);
            }

            // Prints: 9 8 7 6 5 4 3 2 1 0
            // Foreach legal since s implements IEnumerable<int>
            foreach (int n in s)
            {
                System.Console.Write("{0} ", n);
            }
            System.Console.WriteLine();

            // Prints: 9 8 7 6 5 4 3 2 1 0
            // Foreach legal since s.TopToBottom returns IEnumerable<int>
            foreach (int n in s.TopToBottom)
            {
                System.Console.Write("{0} ", n);
            }
            System.Console.WriteLine();

            // Prints: 0 1 2 3 4 5 6 7 8 9
            // Foreach legal since s.BottomToTop returns IEnumerable<int>
            foreach (int n in s.BottomToTop)
            {
                System.Console.Write("{0} ", n);
            }
            System.Console.WriteLine();

            // Prints: 9 8 7 6 5 4 3
            // Foreach legal since s.TopN returns IEnumerable<int>
            foreach (int n in s.TopN(7))
            {
                System.Console.Write("{0} ", n);
            }
            System.Console.WriteLine();

            #endregion
        }