static void Main(string[] args) { Console.WriteLine("***** Fun with the Yield keyword *****\n"); Garage carLot = new Garage(); //get items with GetEnumerator() foreach (Car c in carLot) { Console.WriteLine("{0} is going {1} MPH", c.PetName, c.CurrentSpeed); } Console.WriteLine(); //get dem items in reverse using the named iterator foreach (Car c in carLot.GetTheCars(true)) { Console.WriteLine("{0} is going {1} MPH", c.PetName, c.CurrentSpeed); } Console.ReadLine(); }
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.WriteLine(); Console.WriteLine("=> Using IEnumerator"); IEnumerator i = carLot.GetEnumerator(); i.MoveNext(); Car myCar = (Car)i.Current; Console.WriteLine("{0} is going {1} MPH", myCar.PetName, myCar.CurrentSpeed); Console.WriteLine(); Console.WriteLine("=> Using named iterator"); foreach (Car c in carLot.GetTheCars(true)) { Console.WriteLine("{0} is going {1} MPH", c.PetName, c.CurrentSpeed); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with the Yield Keyword *****\n"); Garage carLot = new Garage(); // This uses the Garage default enumerator Console.WriteLine("Show using the default iterator"); foreach (Car c in carLot) { Console.WriteLine(c); } Console.WriteLine(); // Get items using named iterator. Console.WriteLine("Show using GetTheCars(false)"); foreach (Car c in carLot.GetTheCars(false)) { Console.WriteLine(c); } Console.WriteLine(); // Get items (in reverse!) using named iterator. Console.WriteLine("Show in reverse using GetTheCars(true)"); foreach (Car c in carLot.GetTheCars(true)) { Console.WriteLine(c); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Custom Iterator using yield! *****\n"); Garage carLot = new Garage(); // try // { // IEnumerator carEnumerator = carLot.GetEnumerator(); // } // catch (Exception e) // { // Console.WriteLine($"Exception occured on GetEnumerator"); //} 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(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with the Yield Keyword *****\n"); Garage carLot = new Garage(); //Next line demonstrates the delayed error error //IEnumerator carEnumerator = carLot.GetEnumerator(); // Get items using GetEnumerator(). foreach (Car c in carLot) { Console.WriteLine("{0} is going {1} MPH", c.PetName, c.CurrentSpeed); } Console.WriteLine(); // Get items (in reverse!) using named iterator. foreach (Car c in carLot.GetTheCars(true)) { Console.WriteLine("{0} is going {1} MPH", c.PetName, c.CurrentSpeed); } Console.ReadLine(); }
static void WithYield() { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("=> compare with named iterator"); Garage carLot = new Garage(); foreach (Car c in carLot) { Console.WriteLine($"{c.PetName} is going {c.CurrSpeed} MPH"); } Console.WriteLine("-> using GetTheCars"); foreach (Car c in carLot.GetTheCars(true)) { Console.WriteLine($"{c.PetName} is going {c.CurrSpeed} MPH"); } }
static void Main(string[] args) { Console.WriteLine("****** Fun with the Yield keyword ******\n"); Garage carLot = new Garage(); // Get items using GetEnumerator();. foreach (Car c in carLot) { Console.WriteLine("{0} is going at a speed of {1} KMH", c.PetName, c.CurrSpeed); } Console.WriteLine(); // Get items (in reverse!) using named iterator foreach (Car c in carLot.GetTheCars(true)) { Console.WriteLine("{0} is going at a speed of {1} KMH", c.PetName, c.CurrSpeed); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun wit the Yield Keyword *****\n"); Garage carLot = new Garage(); 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(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with the Yield Keyword *****\n"); Garage carLot = new Garage(); //No exception at this point without local function //IEnumerator carEnumerator = carLot.GetEnumerator(); //try //{ // //Error at this time // carEnumerator.MoveNext(); //} //catch (Exception e) //{ // Console.WriteLine($"Exception occurred on MoveNext"); //} try { //Error at this time var carEnumerator = carLot.GetEnumerator(); } catch (Exception e) { Console.WriteLine($"Exception occurred on GetEnumerator"); } //Get items using GetEnumerator(). foreach (Car c in carLot) { Console.WriteLine("{0} is going {1} MPH", c.PetName, c.CurrentSpeed); } Console.WriteLine(); //Get items(in reverse!) using named iterator. foreach (Car c in carLot.GetTheCars(true)) { Console.WriteLine("{0} is going {1} MPH", c.PetName, c.CurrentSpeed); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("**** Fun with yield keyword ****"); Garage carLot = new Garage(); foreach (Car c in carLot) { Console.WriteLine("{0} is going {1} KPH", c.PetName, c.CurrSpeed); } Console.WriteLine(); Console.WriteLine("Get cars in reversed:"); foreach (Car c in carLot.GetTheCars(true)) { Console.WriteLine("{0} is going {1} KPH", c.PetName, c.CurrSpeed); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with the Yield Keyword *****\n"); Garage carlot = new Garage(); //使用GetEnumerator()获取项 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(); }
static void Main(string[] args) { Garage carLot = new Garage(); // Get items using the GetEnumerator() foreach (Car c in carLot) // same as ( Car c in carLot.GetEnumerator() ) { Console.WriteLine("{0} is goinf {1} MPH", c.PetName, c.CurrentSpeed); } Console.WriteLine(); // Get items in reverse using named iterator foreach (Car c in carLot.GetTheCars(true)) { Console.WriteLine("{0} is goinf {1} MPH", c.PetName, c.CurrentSpeed); } Console.ReadLine(); }