My code is simply:
public override C Calculator<C>(Team[] teams, Func<Team, C> calculatorFunc)
{
return teams.Average(calculatorFunc);
}
I get this error:
Error 2 The type arguments for method ‘System.Linq.Enumerable.Average(System.Collections.Generic.IEnumerable, System.Func)’ cannot be inferred from the usage. Try specifying the type arguments explicitly.
How can I fix this?
You can’t – at least in the current form. There is no
Averageoverload available that works on completely generic values (i.e. for all typesCas you specified).Average needs lists of numbers (
int,double,float…) or a conversion function that produces numbers. In the current form, you could callCalculator<string>and it would make absolutely no sense to compute the average of strings.You’ll just have to restrict the method to a specific numeric type (or provide overloads), but generics simply won’t work.