This question has already been answered:

using System; // Контрвариантность обобщений. // Контрвариантность обобщений в C# 4.0 ограничена делегатами. namespace Generics { class Animal { } class Cat : Animal { } class Program { delegate void MyDelegate<in T>(T a); // in - Для аргумента. public static void CatUser(Animal animal) { Console.WriteLine(animal.GetType().Name); } static void Main() { MyDelegate<Animal> delegateAnimal = new MyDelegate<Animal>(CatUser); //MyDelegate<Cat> delegateCat = delegateAnimal; MyDelegate<Cat> delegateCat=new MyDelegate<Animal>(CatUser); delegateAnimal(new Animal()); delegateCat(new Cat()); delegateAnimal(new Cat()); // delegateCat(new Animal()); // Невозможно. // Delay. Console.ReadKey(); } } } 

why you cannot call delegateCat (new Animal ()); ? Thank you in advance

Reported as a duplicate by Grundy , Qwertiy participants , user194374, VenZell , aleksandr barakin Jul 22 '16 at 1:31 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    2 answers 2

    Because the type of announcement is important, not the runtime type.

     MyDelegate<Cat> delegateCat=new MyDelegate<Animal>(CatUser); 

    Such an assignment is possible, since any function that accepts an animal can take a cat as it.

    - I need a trainer of cats.
    - Universal animal trainer fit?
    - Yes.

    why you cannot call delegateCat(new Animal()); ?

    Because the compiler sees the function that accepts the cat. Potentially, such a function can cause cat methods that are absent in an animal. The compiler does not know whether a function does this — it simply believes a programmer who has written that it is a cat that is needed, and not any animal.

    - We have a great cat trainer.
    “Can he train an unknown animal?”
    - It is not known, so the animal is no good. Come with a cat.

    • 2
      Beautiful analogy, by the way. - VladD

    If simplistic, then Animal cannot replace the type of its successor Cat. A cat parent can. Therefore, in the Callback, a response with the Cat type is expected, and by submitting Animal to the input we get an InvalidCastException or something.