3

Following snippet wouldn't compile. With following error:

Cannot implicitly convert type 'Container<ChildClass>' to 'Container<BaseClass>'

class BaseClass {}
class ChildClass : BaseClass {}
class Container<T> where T : BaseClass {}
class Program {
    static void Main() {
        // why doesn't this work?
        Container<BaseClass> obj = new Container<ChildClass>(); 
    }
}

Is this by design? If it is, what is the reason?

THX-1138
  • 21,316
  • 26
  • 96
  • 160

4 Answers4

14

(made wiki, in case of dups)

C# (3.0) doesn't support covariance of lists etc. C# 4.0 will support limited [co|contra]variance, but still not lists.

The problem is that with:

Container<BaseClass> obj = new Container<ChildClass>(); 

I could do:

obj.Add(new SomeOtherSubclass()); // SomeOtherSubclass : BaseClass

which would compile, but not work.

This behaviour is supported for arrays, but largely for historic reasons.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • A friend of mine complained about lack of covariance like a year ago when generics first came out. I had no idea what he was talking about. Then I tried to do the above... I should listen to him more often. – Min Jun 11 '09 at 14:57
4

Yo,

If you want the killer article on covariance/contravariance on C#, check out the eric lippert blog, "fabulous adventures in coding". First, this is my favortie blog's name, and second eric wrote the best sequence of articles on (co|contra)variance:

http://blogs.msdn.com/ericlippert/archive/2007/10/16/covariance-and-contravariance-in-c-part-one.aspx

This is as good as Breaking Bad.

Roubachof
  • 3,351
  • 3
  • 23
  • 34
2

This is what's knows as covariance / contravariance which isn't available as of C# 3.0. It will be somewhat available in C# 4.0. Here's some info:

http://reddevnews.com/articles/2009/05/01/generic-covariance-and-contravariance-in-c-40.aspx

BFree
  • 102,548
  • 21
  • 159
  • 201
0

Cannot implicitly convert type Container<ChildClass> to Container<BaseClass>

There is a very common mis-conception that MyClass<Child> inherits from MyClass<Base>.

Amy B
  • 108,202
  • 21
  • 135
  • 185