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

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 { ... }

Leave a Comment