我的问题在以下代码中有详细说明 - 我问这个的原因是我正在尝试代表:
//create the delegate
delegate int del(int x);
class Program {
static void Main(string[] args) {
Program p;
p = new Program();
del d = p.a;
d += p.b;
d += p.c;
d += p.d;
d += p.e;
Console.WriteLine(d(10)); //<<was hoping it would be 10+2+3+4+5+6
Console.WriteLine("press [enter] to exit");
Console.ReadLine();
}
private int a(int x) { Console.WriteLine("a is called"); return x + 2; }
private int b(int x) { Console.WriteLine("b is called"); return x + 3; }
private int c(int x) { Console.WriteLine("c is called"); return x + 4; }
private int d(int x) { Console.WriteLine("d is called"); return x + 5; }
private int e(int x) { Console.WriteLine("e is called"); return x + 6; }
}
16 被退回....
所有函数都会触发,因为各种消息“被调用”等都被打印到console
但只返回从最后一个函数e
返回的数量 - 我假设在后台它们被返回但随后被覆盖?