Lets say I have a Base Class:
public class Base{
}
I have a class Derived that inherits from Base Class:
public class Derived: Base{
}
Is it possible to write an extension method, for a List<T>
of classes, that inherit only from the Base class, something like this:
public static string F(this List<Base> baseClass){
return "This class inherits from Base Class";
}
The extension method should work for List<Base>
and List<Derived>
. But it doesn't for me.
public static string F<T>(this List<T> baseClass)
where T : Base
{
return "This class inherits from Base Class";
}
The method must be generic because List<Derived>
is not a subtype of List<Base>
(and not a supertype either).
If IEnumerable
is enough for you and this was an interface or delegate method I think you could solve this with covariance, too.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments