Converting a List of Inherited Type to a List of Base type

For example you have a base class and a class that inherits it like below.

public class A
{ }

public class B : A
{ }

And you have generic list of these types. Then if you want to convert List<B> to List<A>

You can use this code block:

List<B> listB = new List<B>(); 
List<A> listA = listB.Cast<A>().ToList();

 Happy coding.

Posts created 141

Leave a Reply

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top