I've these two classes
public class A {}
public class B : A {}
And the cast from class A
to class B
works fine.
B testB1 = new B();
A testA1 = testB1;
B testB2 = (B)testA1; //this works
But: Why is this cast not working?
List<B> testB1List = new List<B>();
List<A> testA1List = ((IEnumerable<A>)testB1List).ToList();
List<B> testB2List = ((IEnumerable<B>)testA1List).ToList(); //not working
The solution is:
List<B> testB1List = new List<B>();
List<A> testA1List = ((IEnumerable<A>)testB1List).ToList();
List<B> testB2List = testA1List.Cast<B>().ToList();
But why is it like this?