The easiest way to cast 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 types List<Base>. I know that I can just add instances Descendantto List<Base>, but I think it would be preferable to keep a stronger typing here.

What is the easiest way to cast from List<Descendant>to List<Base>?

+3
source share
1 answer

Use LINQ:

List<Descendant> descendants = ...;

descendants.Cast<Base>().ToList();
+5
source

Source: https://habr.com/ru/post/1782894/


All Articles