代理
来源:互联网 发布:我们的存在知乎 编辑:程序博客网 时间:2024/06/08 12:58
代理( delegate )相当于定义一个以函数形式指向函数的指针。其中代理函数必须与被代理函数有相同的参数表。
例如,如下的例子:
// keyword_delegate.cs
// delegate declaration
delegate void MyDelegate(int i);
class Program
{
public static void Main()
{
TakesADelegate(new MyDelegate(DelegateFunction));
}
public static void TakesADelegate(MyDelegate SomeFunction)
{
SomeFunction(21);
}
public static void DelegateFunction(int i)
{
System.Console.WriteLine("Called by delegate with number: {0}.", i);
}
}