将List <Descendant>转换为List <Base>的最简单方法

Ear*_*rlz 3 .net c# collections list

我有这样的类结构:

abstract class Base{}
class Descendant : Base{}
Run Code Online (Sandbox Code Playgroud)

我有另一个需要类型列表的类List<Base>.我知道我可以添加Descendant实例,List<Base>但我认为在这里保持更强的输入是更好的.

什么是铸造的最简单的方法List<Descendant>List<Base>

小智 5

使用LINQ:

List<Descendant> descendants = ...;

descendants.Cast<Base>().ToList();
Run Code Online (Sandbox Code Playgroud)