KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
In response to another question I have tried to do the following. I don't think I interpreted that question correctly, but I do wonder if the below is possible somehow (my attempts have failed) and if not why not: public class MyBaseClass { } public class MyClass : MyBaseClass { } public class B<T> { } public class A<T> : B<T> { } static void Main(string[] args) { // Does not compile B<MyBaseClass> myVar = new A<MyClass>(); } I thought this might be made to work using a generic interface with a covariant type parameter: interface IB<out T> { } public class B<T> : IB<T> { } but I was wrong about that, that does not work either. EDIT As SLaks has pointed out 'Interfaces are covariant; classes are not.' (thanks SLaks). So now my question is why? What was the thinking behind the design (one for Eric Lippert I think) is it not possible, undesirable or is it on a 'maybe one day' list?
Tags (comma-separated)
Save Edits
Cancel