You can do this with a type type Func<>where the first family tree represents the passed type and the second represents the return type of the function:
double integrate(double b, double a, Func<double, double> f)
{
return (b-a) * (f(a) + f(b)) / 2;
}
Your call will look like this:
var a = 1.0;
var b = 2.0;
var result = integrate(b, a, f);
Or, if you prefer the lambda expression:
var result = integrate(b, a, x => x*x + 2*x);
source
share