The code below is just an example
// error CS0305
AddList<IntList>();
// no errors, but specifying T is clearly redundant
// because compiler already knows that IntList is derived from List<int>
AddList<IntList, int>();
void AddList<TList, T>() where TList : List<T>
{
// ...
}
class IntList : List<int> { }
Is there any way to write AddList<IntList>();
instead of AddList<IntList, int>();
?
Edit: I think I should use other example, because inheritance from List creates confusion
CreateGeneric<DerivedGeneric>(); // error
CreateGeneric<DerivedGeneric, int>(); // no error
TGeneric CreateGeneric<TGeneric, T>() where TGeneric : Generic<T>, new()
{
return new TGeneric();
}
class DerivedGeneric : Generic<int> { }
class Generic<T> { }