There's no way to do exactly what you want - the .NET compiler can't partially intuit type parameters for generics. This leaves you with three choices.
Do nothing. The way you have it working now is the standard way to implement something like this. You get the key type you want (
CompositeKey<Tid, TName>
) automatically.Give up the types of
CompositeKey<>
. In this scenario, you accept anyCompositeKey
as aTKey
value. To do this, you need either a non-generic base class forCompositeKey
or anICompositeKey
interface. You will be unable to access the types of theTid
and TNameinside
SpecialCollectioN<>` though, because you're not specifying any types it can use.public class SpecialCollection<TKey, TValue> : Dictionary<TKey, TValue> where TKey : CompositeKey { private Dictionary<TKey, TValue> _baseDictionary = new Dictionary<TKey, TValue>(); }
Specify four type parameters. In this way, you force the first type to be a
CompositeKey<Tid, TName>
, and theSpecialCollection<>
will know what the types of it are. On the other hand, you have to specify all four, because C# can't do partial generic type inference. (i.e. it's all or nothing). Note that you can change the order around to whatever makes the most sense to you.public class SpecialCollection<TKey, TValue, Tid, TName> : Dictionary<TKey, TValue> where TKey : CompositeKey<Tid, TName> { private Dictionary<TKey, TValue> _baseDictionary = new Dictionary<TKey, TValue>(); }