KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Why is the last line not allowed? IEnumerable<double> doubleenumerable = new List<double> { 1, 2 }; IEnumerable<string> stringenumerable = new List<string> { "a", "b" }; IEnumerable<object> objects1 = stringenumerable; // OK IEnumerable<object> objects2 = doubleenumerable; // Not allowed Is this because double is a value type that doesn't derive from object, hence the covariance doesn't work? Does that mean that there is no way to make this work: public interface IMyInterface<out T> { string Method(); } public class MyClass<U> : IMyInterface<U> { public string Method() { return "test"; } } public class Test { public static object test2() { IMyInterface<double> a = new MyClass<double>(); IMyInterface<object> b = a; // Invalid cast! return b.Method(); } } And that I need to write my very own IMyInterface<T>.Cast<U>() to do that?
Tags (comma-separated)
Save Edits
Cancel