public People GetOldest() { int oldestAge = 0; People oldestPeople = new People(); foreach (DictionaryEntry currentPerson in this) { if (((Person)currentPerson.Value).Age > oldestAge) { oldestAge = ((Person)currentPerson.Value).Age; } } foreach (DictionaryEntry currentPerson in this) { if (((Person)currentPerson.Value).Age == oldestAge) { oldestPeople.Add((Person)currentPerson.Value); } } return oldestPeople; }
static void Main(string[] args) { People list = new People(); Person person1 = new Person(); person1.Name = "John"; person1.Age = 47; Person person2 = new Person(); person2.Name = "Alex"; person2.Age = 17; list.Add(person1); list.Add(person2); list.Add(new Person("Lola", 47)); list.Add(new Person("Vlad", 34)); Console.WriteLine("Name: {0}, Age: {1}", list["John"].Name, list["John"].Age); Console.WriteLine("{0} < {1} ? {2}", list["John"].Name, list["Alex"].Name, list["John"] < list["Alex"]); People oldestInList = list.GetOldest(); foreach (DictionaryEntry currentPeople in oldestInList) { Console.WriteLine("Name: {0}, Age: {1}", ((Person)currentPeople.Value).Name, ((Person)currentPeople.Value).Age); } Console.ReadKey(); }