I have generic type that looks like:
public class GenericClass<T, U> where T : IComparable<T>
{
// Class definition here
}
I then have a collection of these instances. What is the cleanest way to pass through the type constraints?
public class GenericCollection<V> where V : GenericClass<T, U> // This won't compile
{
private GenericClass<T, U>[] entries;
public V this[index]
{
get{ return this.entries[index]; }
}
}
Is there perhaps a better way to design this? I think that specifying
GenericCollection<T, U, V> where V : GenericClass<T, U>
seems awkward. Might be my only option though….
When creating a generic class, all generic type parameters of all objects used in all members of the generic class must be resolvable at compile time.
You can have type parameters that are specific instances of other generic types – for example:
But if you want the type parameters of
GenericClassto be generic themselves, then all of those types need to be part of the class declaration, as you’ve written:Sorry if it seems awkward, but that’s your only option.