using System;
namespace DelegateTest
{
public delegate string MyMethodDelegate(int i);
class Program
{
static void Main(string[] args)
{
TestClass tc = new TestClass();
MyMethodDelegate myDeleg1 = new MyMethodDelegate(tc.CallBack2);
MyMethodDelegate myDeleg2 = new MyMethodDelegate(TestClass.CallBack1);
CallDelegate(myDeleg1);
CallDelegate(myDeleg2);
CallDelegate(new MyMethodDelegate(delegate(int i){
return "匿名函数-"+i;
}));
Console.ReadKey();
}
static void CallDelegate(Delegate deleg)
{
Console.WriteLine( deleg.DynamicInvoke(9) );
}
}
class TestClass
{
public static string CallBack1(int i)
{
return "CallBack1-"+i;
}
public string CallBack2(int i)
{
return "CallBack2-"+i;
}
}
}
运行结果