开发者

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜