Lets say I've got a generic interface IFace<T, U>
. The T
and U
can be constrained in some additional way, but I don't think this is relevant to the problem.
I'd like to create a new generic class GenClass
with the following definition:
class GenClass<T> where T: IFace
I.e. I'd like to tell the compiler that GenClass
should handle any implementation of IFace
without specifying the generic types.
Is this possible, or do I NEED to specify the T
and U
generic parameters?
I've tried writing IFace
as well as IFace<>
or IFace<,>
, but the compiler always threw a fit - so I'm thinking this cannot be done. Still, perhaps I'm going about this the wrong way?
Is this possible, or do I NEED to specify the T and U generic parameters?
You need them, I'm afraid. You'll need to have
class GenClass<T, TX, TY> where T : IFace<TX, TY>
That's assuming you can't create a non-generic IFace
interface that the generic one derives from - that's often the simplest approach:
public interface IFace
{
// Include anything that doesn't need the type parameters here
}
public interface IFace<T1, T2> : IFace
{
// Include anything that needs the type parameters here
}
Then you can have:
class GenClass<T> where T : IFace
because now the non-generic interface really does exist.
See more on this question at Stackoverflow