C# generic “where constraint” with “any generic type” definition?

前端 未结 2 1365
醉酒成梦
醉酒成梦 2020-11-27 13:48

Let me give example:

  1. I have some generic class/interface definition:

    interface IGenericCar< T > {...}

  2. I have anot

相关标签:
2条回答
  • 2020-11-27 14:34

    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 { ... }
    
    0 讨论(0)
  • 2020-11-27 14:50

    Would it make any sense to do something like:

    interface IGenericCar< T > {...}
    interface IGarrage< TCar, TCarType > 
        where TCar: IGenericCar< TCarType > {...}
    
    0 讨论(0)
提交回复
热议问题