Easiest way of casting List<Descendant> to List<Base>
I have a class structure like this:
abstract class Base{}
class Descendant : Base{}
I have another class that needs a list of type List<Base>
. I know I can just add 开发者_StackOverflow中文版Descendant
instances to a List<Base>
but I'd think it'd be preferable to keep stronger-typing here.
What is the easiest way of casting from List<Descendant>
to List<Base>
?
Use LINQ:
List<Descendant> descendants = ...;
descendants.Cast<Base>().ToList();
精彩评论