Ejemplo 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();
        }
Ejemplo n.º 2
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();
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Garage carLot = new Garage();

            var i = carLot.GetEnumerator();

            while (i.MoveNext())
            {
                Car car = (Car)i.Current;
                Console.WriteLine($"{car.PetName} is going {car.CurrentSpeed} MPH");
            }
            Console.WriteLine();

            foreach (Car car in carLot)
            {
                Console.WriteLine($"{car.PetName} is going {car.CurrentSpeed} MPH");
            }
            Console.WriteLine();

            foreach (Car car in carLot.GetTheCars(true))
            {
                Console.WriteLine($"{car.PetName} is going {car.CurrentSpeed} MPH");
            }
            Console.ReadLine();
        }
Ejemplo n.º 4
0
 static void Main(string[] args)
 {
     Garage g = new Garage();
     
     foreach(Car c in g.GetTheCars(true))
     {
         
     }
 }
Ejemplo n.º 5
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();

        }
Ejemplo 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();
        }
Ejemplo 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);
            //}
            foreach (Car c in carLot)
            {
                Console.WriteLine("{0} is going {1} MPH", c.PetName, c.CurrentSpeed);
            }
            Console.WriteLine();

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

            Console.ReadLine();
        }