c# - Colletion with derived class parameter -
c# - Colletion with derived class parameter -
i have classes , method
public class base{ // members;} public class derived : base{// members} mymethod(ienumerable<base> in1);
try phone call mymethod
:
mymethod(ienumerable<derived > in2);
and compilation error? how should solve that
what you're expecting called covariance. means actual generic type more specific required one. can see covariance not work generic classes: list<derived>
should not assignable list<base>
, because happen if add together base
intances list, list of derived
instances? ienumerable
covariant , ilist
invariant.
this concept has been applied collection classes in .net 4.0. when using version of .net < 4, classes invariant.
c# inheritance ienumerable
Comments
Post a Comment