public class NumberOption {
public static double NumOne(double x) { return x * 2; }
public static double NumTwo(double x) { return x * x; }
}
delegate double DoubleOperation(double x);
static void PrintNumber(DoubleOperation op, double x) {
double result = op(x);
Console.WriteLine("Value is {0}, result of DoubleOperation is {1}", x, result);
}
static void Main() {
DoubleOperation d1 = new DoubleOperation(NumberOption.NumOne);
DoubleOperation d2 = new DoubleOperation(NumberOption.NumTwo);
PrintNumber(d1, 1.5); // Value is 1.5, result ... 3
PrintNumber(d2, 3.2); // Value is 3.2, result ... 10.24
}
delegate double DoubleOperation(double x);
static void PrintNumber(DoubleOperation op, double x) { ... }
static void Main() {
DoubleOperation d1 = delegate(double x) { return x * 2; };
DoubleOperation d2 = delegate(double x) { return x * x; };
PrintNumber(d1, 1.5);
PrintNumber(d2, 3.2);
}
委派可串聯多個方法,依序呼叫。後續應用再詳細說明。