static void Main(string[] args) { Console.WriteLine("***** Custom Person Collection *****\n"); PeopleCollection myPeople = new PeopleCollection(); myPeople.AddPerson(new Person("Homer", "Simpson", 40)); myPeople.AddPerson(new Person("Marge", "Simpson", 38)); myPeople.AddPerson(new Person("Lisa", "Simpson", 9)); myPeople.AddPerson(new Person("Bart", "Simpson", 7)); myPeople.AddPerson(new Person("Maggie", "Simpson", 2)); // This would be a compile-time error! // myPeople.AddPerson(new Car()); foreach (Person p in myPeople) { Console.WriteLine(p); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Indexers *****\n"); PeopleCollection myPeople = new PeopleCollection(); myPeople[0] = new Person("Homer", "Simpson", 40); myPeople[1] = new Person("Marge", "Simpson", 38); myPeople[2] = new Person("Lisa", "Simpson", 9); myPeople[3] = new Person("Bart", "Simpson", 7); myPeople[4] = new Person("Maggie", "Simpson", 2); // Now obtain and display each item using indexer. for (int i = 0; i < myPeople.Count; i++) { Console.WriteLine("Person number: {0}", i); Console.WriteLine("Name: {0} {1}", myPeople[i].FirstName, myPeople[i].LastName); Console.WriteLine("Age: {0}", myPeople[i].Age); Console.WriteLine(); } UseGenericListOfPeople(); Console.ReadLine(); }