public class A {}
public class B : A {}
now what the best way to get this working
List<A> a;
List<B> b = new List<B>();
a = b; // throw Cannot convert List<B> to List<A>
Thank you
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
List<T>type doesn’t support covariance, so you can’t assign aList<B>directly to aList<A>even thoughBitself is directly assignable toA. You’ll need to do a pass through listb, converting and adding the items into listaas you go. TheConvertAllmethod is a convenient way to do this: