If the Covariant type parameters which added in .Net Framework 4 enable me to make assignments that look like polymorphism
Why I can write
IEnumerable<object> lst = new List<string>();
And I can’t write
List<object> lst = new List<string>();
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.
There are 2 reasons why it doesn’t work:
IEnumerable<T>is covariant (it is declaredpublic interface IEnumerable<out T>), butIList<T>isn’t. Otherwise you would be able to do something like that:Covariance and contravariance only work on interfaces, not on concrete classes
From MSDN: