No, this is not legal in C #. C # 4 and above support covariance and contravariance of common interfaces and common delegates when they are built using reference types. So, for example, IEnumerable<T>is covariant, so you can say:
List<Giraffe> giraffes = new List<Giraffe>() { ... };
IEnumerable<Animal> animals = giraffes;
but not
List<Animal> animals = giraffes;
Because the tiger can be included in the list of animals, but the list of giraffes cannot be.
Do a web search on covariance and contravariance in C # and you will find many articles on it.
source
share