static void Main(string[] args) { Console.WriteLine("***** Fun with Indexers *****\n"); PersonCollection myPeople = new PersonCollection(); // Add objects with indexer syntax. 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(); } PersonCollectionStringIndexer myPeopleStrings = new PersonCollectionStringIndexer(); myPeopleStrings["Homer"] = new Person("Homer", "Simpson", 40); myPeopleStrings["Marge"] = new Person("Marge", "Simpson", 38); // Get "Homer" and print data. Person homer = myPeopleStrings["Homer"]; Console.WriteLine(homer.ToString()); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Indexers *****\n"); // PersonCollection myPeople = new PersonCollection(); // // myPeople[0] = new Person(40, "Homer", "Simpson"); // myPeople[1] = new Person(38, "Marge", "Simpson"); // myPeople[2] = new Person(9, "Lisa", "Simpson"); // myPeople[3] = new Person(7, "Bart", "Simpson"); // myPeople[4] = new Person(2, "Maggie", "Simpson"); // // 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(); PersonCollectionStringIndexer myPeopleStrings = new PersonCollectionStringIndexer { ["Homer"] = new Person(40, "Homer", "Simpson"), ["Marge"] = new Person(38, "Marge", "Simpson") }; Person homer = myPeopleStrings["Homer"]; Console.WriteLine(homer); Console.ReadLine(); }