static void Main(string[] args) { Console.WriteLine("***** Custom Generic Collection *****\n"); // // Confusing at best... // CarCollection<int> myInts = new CarCollection<int>(); // myInts.AddCar(5); // myInts.AddCar(10); // myInts.AddCar(11); // Make a collection of Cars. CarCollection <Car> myCars = new CarCollection <Car>(); myCars.AddCar(new Car("Rusty", 20)); myCars.AddCar(new Car("Zippy", 90)); myCars.AddCar(new SportsCar("Viper", 100)); foreach (Car c in myCars) { Console.WriteLine("PetName: {0}, Speed: {1}", c.PetName, c.Speed); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Custom Generic Collection *****\n"); // Make a collection of Cars. CarCollection <Car> myCars = new CarCollection <Car>(); myCars.AddCar(new Car("Rusty", 20)); myCars.AddCar(new Car("Zippy", 90)); foreach (Car c in myCars) { Console.WriteLine("PetName: {0}, Speed: {1}", c.PetName, c.Speed); } Console.WriteLine(); #region Odd ball type param for CarCollection! // This is syntactically correct, but confusing at best! //CarCollection<int> myInts = new CarCollection<int>(); //myInts.AddCar(5); //myInts.AddCar(11); //foreach (int i in myInts) //{ // Console.WriteLine("Int value: {0}", i); //} #endregion // CarCollection<Car> can hold any type deriving from Car. CarCollection <Car> myAutos = new CarCollection <Car>(); myAutos.AddCar(new MiniVan("Family Truckster", 55)); myAutos.AddCar(new SportsCar("Crusher", 40)); foreach (Car c in myAutos) { Console.WriteLine("Type: {0}, PetName: {1}, Speed: {2}", c.GetType().Name, c.PetName, c.Speed); } Console.ReadLine(); }