Esempio n. 1
0
        public static void Run()
        {
            // Contravariance - permits use a method with less derived types in argument for a delegate.

            // delegate requires Animal and method requires Animal
            FeedAnimal feedAnimalWater = DrinkWater;

            feedAnimalWater(new Animal());
            feedAnimalWater(new Cat());

            // delegate requires Animal + method requires Cat
            // It is not possible to pass any animal if we expect only cats.
            //FeedAnimal feedAnimalMilk = DrinkMilk;

            // delegate requires Cat and method requires Animal
            // It is possible because of Contravariance - A cat can drink water.
            FeedCat feedCatWater = DrinkWater;

            feedCatWater(new Cat());

            // delegate requires Cat and method requires Cat
            FeedCat feedCatMilk = DrinkMilk;

            feedCatMilk(new Cat());
        }
Esempio n. 2
0
        private void Btn_feedAnimal_Click(object sender, EventArgs e)
        {
            Animal foundAnimal = new Animal();

            if ((foundAnimal = FindAnimal(SaveAnimalName)) != null)
            {
                FeedAnimal feedAnimal = new FeedAnimal(foundAnimal, ListOfAnimals);
                saveFedFoodAmount = feedAnimal.FoodAmount;
                var result = feedAnimal.ShowDialog();
            }
            else
            {
                MessageBox.Show("Tier wurde nicht gefunden.", "", MessageBoxButtons.OK);
            }
        }