127

Let me give example:

  1. I have some generic class/interface definition:

    interface IGenericCar< T > {...}

  2. I have another class/interface that I want to relate with class above, for example:

    interface IGarrage< TCar > : where TCar: IGenericCar< (**any type here**) > {...}

Basically, I want my generic IGarrage to be dependent on IGenericCar, regardless if it's IGenericCar<int> or IGenericCar<System.Color>, because I don't have any dependency to that type.

Vegard Larsen
  • 12,827
  • 14
  • 59
  • 102
Nenad
  • 24,809
  • 11
  • 75
  • 93

2 Answers2

147

There are typically 2 ways to achieve this.

Option1: Add another parameter to IGarrage representing the T which should be passed into the IGenericCar<T> constraint:

interface IGarrage<TCar,TOther> where TCar : IGenericCar<TOther> { ... }

Option2: Define a base interface for IGenericCar<T> which is not generic and constrain against that interface

interface IGenericCar { ... }
interface IGenericCar<T> : IGenericCar { ... }
interface IGarrage<TCar> where TCar : IGenericCar { ... }
Nestor
  • 8,194
  • 7
  • 77
  • 156
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 6
    Ok, but what should I do if I need to use my generic type `T` inside the `IGarage`? I can't see any possibility in option2. The best solution would be if `IGarage` found type `T` by analysing type `TCar`. – pt12lol May 16 '15 at 07:32
  • 2
    For posterity, a type CAN be created that has a type parameter of a raw generic type, but only with reflection at runtime, and the created class could never be constructed, because the raw generic type parameter could never be constructed automatically without a full definition of ITS respective type parameter. I don't see where this could be useful, except in cases where super-generic static members of the outermost class (i.e. `IGarage>.TellMeAboutCarsInGeneral()`, which would probably be the result of poor design), but I've done it in my tinkering, and it is possible. – Michael Hoffmann Feb 01 '16 at 22:44
  • I assume anyone can add the IGenericCar interface to a class and break the constrained method with an unexpected class. – N-ate Mar 28 '18 at 04:21
  • 2
    @pt12lol: If `IGarrage` actually handles the underlying generic type (e.g. it handles a property of said type), then it needs to know the type, which requires you to specify the type, which is option 1 (the only viable option then). However, if `IGarrage` does not directly handle the underlying generic type (all the `IGarrage` code is agnostic of this underlying type), then option 2 is valid. – Flater May 16 '18 at 21:10
6

Would it make any sense to do something like:

interface IGenericCar< T > {...}
interface IGarrage< TCar, TCarType > 
    where TCar: IGenericCar< TCarType > {...}
snarf
  • 2,684
  • 1
  • 23
  • 26