If I would like to write a method that accepts a variable number "TDerived", where TDerived is any subclass of the "Base" class, is there any way to do this?
The following code only works with one specific subclass specified:
void doStuff<TDerived>(params TDerived[] args) where TDerived : Base
{
}
those. if I
class Super { }
class Sub0 : Super { }
class Sub1 : Super { }
then i can't do
Sub0 s0 = new Sub0();
Sub1 s1 = new Sub1();
doStuff(s0, s1);
since I get "the best overloaded match ... has some invalid arguments".
Regardless of how the compiler handles type constraints and variational functions, this seems (as far as I can tell) completely type safe. I know I can quit, but if it's a safe type, why not let it?
EDIT:
Perhaps a more convincing example:
void doStuff<TDerived>(params SomeReadOnlyCollection<TDerived>[] args) where TDerived : Base
{
foreach(var list in args)
{
foreach(TDerived thing in list)
{
}
}
}