Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with IEnumerable / IEnumerator *****\n");
            Garage carLot = new Garage();

            // Hand over each car in the collection?
            foreach (Car c in carLot)
            {
                Console.WriteLine("{0} is going {1} MPH",
                  c.PetName, c.CurrentSpeed);
            }

            // Use named iterator
            Console.WriteLine();
            foreach (Car c in carLot.GetTheCars(true))
            {
                Console.WriteLine("{0} is going {1} MPH",
                  c.PetName, c.CurrentSpeed);
            }

            // Manually work with IEnumerator.
            Console.WriteLine();
            IEnumerator i = carLot.GetEnumerator();
            i.MoveNext();
            Car myCar = (Car)i.Current;
            Console.WriteLine("{0} is going {1} MPH", myCar.PetName, myCar.CurrentSpeed);

            Console.ReadLine();
        }
Exemplo n.º 2
0
 static void Main(string[] args)
 {
     Garage g = new Garage();
     
     foreach(Car c in g.GetTheCars(true))
     {
         
     }
 }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            Garage carLot = new Garage();
            IEnumerator i = carLot.GetEnumerator();
            
            // Car myCar = (Car)i.Current;  // 枚举尚未开始,请调用MoveNext()
            //bool flag = i.MoveNext();
            //Car myCar = (Car)i.Current;
            //Console.WriteLine("{0} is going {1} MPH", myCar.PetName, myCar.CurrentSpeed);
            for(bool flag = i.MoveNext(); flag == true; flag = i.MoveNext())
            {
                Car myCar = (Car)i.Current;
                Console.WriteLine("CustomForMethod : {0} is going {1} MPH", myCar.PetName, myCar.CurrentSpeed);
            }
            
            // i.Reset();不会对下一次的foreach造成影响,应该是在使用foreach方法的第一步是调用Reset()进行设置,

            foreach (Car c in carLot)
            {
                Console.WriteLine("{0} is going {1} MPH", c.PetName, c.CurrentSpeed);
            }

            foreach (Car c in carLot.GetTheCars(true))
            {
                Console.WriteLine("{0} is going {1} MPH", c.PetName, c.CurrentSpeed);
            }

            Car[] myAutos = new Car[5];
            myAutos[0] = new Car("Rusty", 80, 1);
            myAutos[1] = new Car("Mary", 40, 234);
            myAutos[2] = new Car("Viper", 40, 34);
            myAutos[3] = new Car("Mel", 40, 4);
            myAutos[4] = new Car("Chucky", 40, 5);

            foreach (Car c in myAutos)
            {
                Console.WriteLine("Car: {0}, ID: {1}", c.PetName, c.CarID);
            }
            Console.WriteLine("After NormalSort:");
            Array.Sort(myAutos);

            foreach (Car c in myAutos)
            {
                Console.WriteLine("Car: {0}, ID: {1}", c.PetName, c.CarID);
            }

            Array.Sort(myAutos, Car.SortByPetName);
            Console.WriteLine("After PetNameSort:");
            foreach (Car c in myAutos)
            {
                Console.WriteLine("Car: {0}, ID: {1}", c.PetName, c.CarID);
            }
            Console.ReadLine();

        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            Garage carLot = new Garage();

            foreach (Car c in carLot)
            {
                Console.WriteLine("{0} is going {1} MPH", c.PetName, c.CurrentSpeed);
            }

            Console.ReadKey();
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with IEnumerable / IEnumerator *****\n");
            Garage carLot = new Garage();

            // Hand over each car in the collection?
            foreach (Car c in carLot)
            {
                Console.WriteLine("{0} is going {1} MPH", c.PetName, c.CurrentSpeed);
            }

            Console.ReadKey();
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with IEnumerable / IEnumerator *****");
            Garage carLot = new Garage();
            foreach(Car c in carLot)
                Console.WriteLine("{0} is going {1} MPH", c.PetName, c.CurrentSpeed);

            foreach (Car c in carLot.GetTheCars(true)) 
            {
                Console.WriteLine("{0} is going {1} MPH", c.PetName, c.CurrentSpeed);
            }
            Console.ReadLine();
        }
Exemplo n.º 7
0
        static void Main(string[] args) {

            Garage carLot = new Garage();

            foreach (Car c in carLot) {
                Console.WriteLine("{0} is going {1} MPH", c.PetName, c.CurrentSpeed);
            }

            IEnumerator i = carLot.GetEnumerator();
            i.MoveNext();
            Car myCar = (Car)i.Current;
            Console.WriteLine("{0} is going {1} MPH", myCar.PetName, myCar.CurrentSpeed);
            Console.ReadLine();
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with IEnumerable/IEnumerator *****\n");
            Garage carLot = new Garage();
            //foreach (Car c in carLot)
            //{
            //    Console.WriteLine("{0} is going {1} MPH", c.PetName, c.CurrentSpeed);
            //}

            //manually working with IEnumator is OK bc it was declared public
            IEnumerator i = carLot.GetEnumerator();
            i.MoveNext();
            Car myCar = (Car)i.Current;
            Console.WriteLine("{0} is going {1} MPH", myCar.PetName, myCar.CurrentSpeed);

            Console.ReadLine();
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with IEnumerable / IEnumerator *****\n");
            Garage carLot = new Garage();

            // Hand over each car in the collection?
            foreach (Car c in carLot)
            {
                Console.WriteLine("{0} is going {1} MPH",
                                  c.PetName, c.CurrentSpeed);
            }

            Console.WriteLine();

            // Manually work with IEnumerator.
            IEnumerator i = carLot.GetEnumerator();

            i.MoveNext();
            Car myCar = (Car)i.Current;

            Console.WriteLine("{0} is going {1} MPH", myCar.PetName, myCar.CurrentSpeed);

            Console.ReadLine();
        }