开发者

Easiest way of casting List<Descendant> to List<Base>

开发者 https://www.devze.com 2023-02-02 10:16 出处:网络
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中文版

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消