How do I upcast a collection of base class in C#?

c#

Solution

If you're using C# 3.0 (Visual Studio 2008), you can:

using System.Linq;

List<B> bList = foo.GetListOfA().Cast<B>().ToList();

Problem

In C#, I have a class A that I cannot modify. From this class I've created class B (i.e. inherited from A) which adds some properties. I also have a static function that returns List`<A>` when GetListOfA() is called. How can I cast the return from GetListOfA() to List`<B>`? e.g. List`<B>` bList = foo.GetListOfA() as List`<B>`

Original source