Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Cat c1 = new Cat("c1", 1, "male");

            Console.WriteLine(c1.ProduceSound());
            Dog d2 = new Dog("d2", 2, "male");

            Console.WriteLine(d2.ProduceSound());
            Frog f3 = new Frog("f3", 3, "female");

            Console.WriteLine(f3.ProduceSound());
            Kitten k4 = new Kitten("k4", 4);

            Console.WriteLine(k4.ProduceSound());
            Tomcat t5 = new Tomcat("t5", 5);

            Console.WriteLine(t5.ProduceSound());

            List <Animal> animals = new List <Animal>();

            animals.Add(c1);
            animals.Add(d2);
            animals.Add(f3);
            animals.Add(k4);
            animals.Add(t5);

            var dogsAge =
                from animal in animals
                where animal is Dog
                select animal.Age;

            int dogsAverage = dogsAge.Sum() / dogsAge.Count();

            Console.WriteLine(dogsAverage);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Dog dog1 = new Dog("Jhony", 10, "male");
            Cat cat1 = new Cat("Kotka", 1, "female");
            Frog frog1 = new Frog("Green", 99, "male");
            Kitten kitten1 = new Kitten("Pussy", 18);
            Tomcat tomcat1 = new Tomcat("Tom", 22);
            Dog dog2 = new Dog("Balkan", 1, "male");
            Frog frog2 = new Frog("Karpat", 12, "male");
            Kitten kitten2 = new Kitten("Kitenik", 22);
            Dog dog3 = new Dog("StreetFighter", 5, "female");

            Animal[] animals = new Animal[9];

            animals[0] = dog1;
            animals[1] = cat1;
            animals[2] = frog1;
            animals[3] = kitten1;
            animals[4] = tomcat1;
            animals[5] = dog2;
            animals[6] = frog2;
            animals[7] = kitten2;
            animals[8] = dog3;

            foreach (var kind in animals.GroupBy(a => a.GetType().Name))
            {
                double avrAge = kind.Select(a => a.Age).Average();
                Console.WriteLine("Animal : {0} - AverageAge: {1}", kind.Key, avrAge);
            }
        }