0

I have the following C# classes

public class BaseRow
{ }

public class BaseCollection<T> where T : BaseRow
{ }

public class ClientBaseClass<T> where T : BaseCollection<BaseRow>
{ }

public class ActualRow : BaseRow
{ }

public class ActualCollection : BaseCollection<ActualRow>
{ }

public class ActualClientClass : ClientBaseClass<ActualCollection>
{ }

In compilation of ActualClientClass, the compiler fails saying something like

ActualCollection must be convertible to BaseCollection <BaseRow> in order to use it as parameter T in the generic class.

I am not sure I understand this, as ActualCollection inherits from BaseCollection<ActualRow> where ActualRow inherits from BaseRow. I am having a hard time finding a similar case on the web.

Is this possible at all or do I need to re-define my architecture?

ebug
  • 451
  • 3
  • 18
  • `BaseCollection` does not inherit from `BaseCollection` because `BaseCollection` is not covariant. you could _try_ to switch to interfaces that are covariant but without knowing what the methods are we can't know if its possible. – D Stanley Nov 02 '16 at 15:34
  • @DStanley so, inheritance is an exact duplicate of instantiation / assignment? I don't think so. – grek40 Nov 02 '16 at 15:46
  • 1
    @grek40 If you can find a better duplicate please add a comment. The root problem of both questions is lack of covariance. – D Stanley Nov 02 '16 at 15:48
  • 1
    @DStanley ... and the solution for both problems is probably quite different. I'm not gonna look for dupes right now, I'd rather answer with related code that would work, if noone else finds a suitable duplicate. – grek40 Nov 02 '16 at 15:50
  • @grek40 Actually the solution is probably the same - use covariant interfaces. If you want to add an answer you're free to vote to reopen the question. – D Stanley Nov 02 '16 at 15:53
  • @DStanley I see you are quite detached from the struggles of the typical SO user. [Privilege type: Moderation privilege; Awarded at: 3,000 reputation](http://stackoverflow.com/help/privileges/close-questions). Flagged for moderator attention instead. – grek40 Nov 02 '16 at 16:03

0 Answers0