if i have :
public class A { public int a1;}
public class B : A { public int b1;}
void myFunc(A a)
{}
static void Main(string[] args)
{
B b = new B();
myFunc(b);
}
in myFunc
, a
can access b
object but it can reference only (without cast) to a region in memory which is type A
.
that is understood.
HOwever in covariance it seems that a
can also access b
:
As you can see - it accepts Enumerable of A
and it still can access its B
typed objects
questions:
1) Ok, How behind the scenes it is working ? how can an A
reference can show me a larger
object ?
2) What if i wanted to see in the function the a1
property from the A
class ?what should I change ?
edit
covariance related:
Before C# 4, you couldn't pass in List: cannot convert from 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'