Example #1
0
        static void Main()
        {
            Animal[] animals = new Animal[]
            {
                new Dog("Doggy", 3, Gender.Male),
                new Frog("Princess", 2, Gender.Female),
                new Kitten("Milo", 1),
                new Tomcat("Fet", 4),
                new Kitten("Mika", 3),
                new Tomcat("Bob", 5),
                new Dog("Doge", 6, Gender.Male),
                new Dog("Izzy", 4, Gender.Female),
                new Frog("Mich", 3, Gender.Male)
            };

            animals[1].ProduceSound();
            animals[3].ProduceSound();
            animals[6].ProduceSound();
            Console.WriteLine();

            var animalsAverageAge =
                from animal in animals
                group animal by animal.GetType().Name into grp
                select new
                {
                    GroupName = grp.Key,
                    AverageAge = grp.Average(a => a.Age)
                };

            foreach (var animal in animalsAverageAge)
            {
                Console.WriteLine("{0}: average age: {1:0.00}", animal.GroupName, animal.AverageAge);
            }
        }
 public static double AverageAge(Animal[] animals)
 {
     int sum = 0;
     for (int i = 0; i < animals.Length; i++)
     {
         sum += animals[i].Age;
     }
     return (double)sum / animals.Length;
 }