Why can I not convert a Foo<Bar> to IFoo<IBar>.
If I try I get:
There is no implicit reference conversion from
Foo<Bar>toIFoo<IBar>
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.
It would work if you were using C# 4 and
IFoowere declared as:assuming that
BarimplementsIBarandFoo<T>implementsIFoo<T>.However, it could only be declared that way if it were safe. It’s not safe if
Tvalues “go into” the API as well as coming out. For example:This can’t be covariant in T, as otherwise you could write:
Read Eric Lippert’s long blog series on generic variance for much more information.