I’m just wondering why passing a System.Collections.Generic.List<string> into this function test(ICollection<object> t) will not work, why can’t I pass it in like passing a string into test2(object t)?
Doesn’t make much sense to me!
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.
Because
ICollectionisn’t an output only interface, it is not covariant.Consider this code:
What is supposed to happen when
testtries to stuff aTextBoxinto aList<string>?The contract for
ICollection<object>is that anyobjectcan be put in, and items coming out will always be of typeobject. ButList<string>only meets half that contract.