How can link A show me a larger object?
First of all, it is a smaller type. Every giraffe is an animal, but not every animal is a giraffe. Therefore, there are fewer giraffes in the world than there are animals in the world. Therefore, a giraffe is a smaller type than an animal.
Your type B is a smaller type than A. And, of course, a reference to a larger type may refer to something of a smaller type.
This has nothing to do with covariance. It always happens that IEnumerable<A> can give you B :
List<A> myList = new List<A>() { new B(); }
The list of animals may contain a giraffe. This has nothing to do with covariance.
Similarly, a link can always give you a smaller type back:
A a = new B();
to those who say that this has nothing to do with covariance ...
Whatever sequence A may contain , B has nothing to do with covariance. What is involved with covariance is that sequence B can be converted to sequence A by reference conversion. Before covariant transforms were added in C # 4, this transform would fail.
What if I want to see property a1 from class A in a function? what should i change?
You do not have to change anything; he is already working. Give it a try.
source share