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(); }
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(); }
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(); }
public 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(); Console.Write("Press any key to continue . . . "); Console.ReadKey(true); }
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(); }
static void Main(string[] args) { Garage carLot = new Garage(); // 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); }
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(); }
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(); }
static void Main(string[] args) { Garage carLot = new Garage(); foreach (Car c in carLot) { Console.WriteLine("{0} is going {1}", c.PetName, c.CurrentSpeed); } IEnumerator i = carLot.GetEnumerator(); i.MoveNext(); Car curCar = (Car)i.Current; Console.WriteLine("{0} is going {1}", curCar.PetName, curCar.CurrentSpeed); Console.ReadLine(); }
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} KPH", c.PetName, c.CurrSpeed); } Console.WriteLine(); IEnumerator iEnum = carLot.GetEnumerator(); iEnum.MoveNext(); Car myCar = (Car)iEnum.Current; Console.WriteLine("{0} is going {1}", myCar.PetName, myCar.CurrSpeed); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with IEnumerable / IEnumerator *****"); Garage carLot = new Garage(); // Work with IEnumerator indirectly. foreach (Car c in carLot) { Console.WriteLine("{0} is going {1} MPH", c.PetName, c.CurrSpeed); } // 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.CurrSpeed); Console.ReadLine(); }
static void Enumerators() { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("=> custom enumerator"); Garage carLot = new Garage(); foreach (Car c in carLot) { Console.WriteLine($"{c.PetName} is going {c.CurrSpeed} MPH"); } Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine("-> use custom enumerator manually"); IEnumerator enumerator = carLot.GetEnumerator(); while (enumerator.MoveNext()) { Car c = (Car)enumerator.Current; Console.WriteLine($"{c.PetName} is going {c.CurrSpeed} MPH (M)"); } }