I was reading a bit on generic variance and I don't have a full understanding of it yet but I'd like to know if it makes something like the following possible?
class A<T> { }
class B { }
class C : B { }
class My1 {
public My1(A<B> lessDerivedTemplateParameter)
{
}
}
class My2 : My1 {
public My2(A<C> moreDerivedTemplateParameter)
: base(moreDerivedTemplateParameter) // <-- compile error here, cannot convert
{
}
}
No, because while
C
inherits fromB
,A<C>
does not inherit fromA<B>
.To understand why this is the case, imagine if
A<T>
were insteadList<T>
:Now on the other hand, this is legal: