# 委托Delegates

using System;
delegate double Function(double x);
class Multiplier
{
double factor;
public Multiplier(double factor)
{
this.factor = factor;
}
public double Multiply(double x)
{
return x * factor;
}
}
class DelegateExample
{
static double Square(double x)
{
return x * x;
}
static double[] Apply(double[] a, Function f)
{
double[] result = new double[a.Length];
for (int i = 0; i < a.Length; i++) result[i] = f(a[i]);
return result;
}
static void Main()
{
double[] a = {0.0, 0.5, 1.0};
double[] squares = Apply(a, Square);
double[] sines = Apply(a, Math.Sin);
Multiplier m = new Multiplier(2.0);
double[] doubles =  Apply(a, m.Multiply);
}
}


Function 委托类型实例可以引用需要使用 double 自变量并返回 double 值的方法。An instance of the Function delegate type can reference any method that takes a double argument and returns a double value. Apply 方法将给定的函数应用于 double[] 的元素，从而返回包含结果的 double[]The Apply method applies a given Function to the elements of a double[], returning a double[] with the results. Main 方法中，Apply 用于向 double[] 应用三个不同的函数。In the Main method, Apply is used to apply three different functions to a double[].

double[] doubles =  Apply(a, (double x) => x * 2.0);