(*) 委託
從最簡單的例子開始:
namespace ConsoleApplication1
{
class Program
{
// 委託其實就相當於一個類型。這裡,類型的名字叫BinaryOp
public delegate int BinaryOp(int x, int y);
static public int Add(int x, int y)
{
return x + y;
}
static void Main()
{
// 建立一個BinaryOp類型的執行個體,用靜態Add方法初始化
BinaryOp d = new BinaryOp(Add);
Console.WriteLine(d(10, 10));
}
}
}
上面是靜態方法的委託,下面再來看一個執行個體方法的委託。
class MyClass
{
private string name;
public MyClass(string name)
{
this.name = name;
}
public void DisplayName()
{
Console.WriteLine("{0}", name);
}
}
class Program
{
// 委託其實就相當於一個類型。這裡,類型的名字叫SimpleDelegate
public delegate void SimpleDelegate();
static void Main()
{
MyClass a = new MyClass("A");
MyClass b = new MyClass("B");
// 用執行個體方法DisplayName初始化
SimpleDelegate d = new SimpleDelegate(a.DisplayName);
d();
d = new SimpleDelegate(b.DisplayName);
d();
}
}
(*) 事件
委託是個類型,而事件是個成員。看下面的代碼:
namespace ConsoleApplication1
{
public class SimpleMath
{
public delegate int BinaryOp(int a, int b); // 定義Binary類型
public event BinaryOp BinaryEvent; // 定義BinaryEvent成員
public int Add(int a, int b) { return a + b; }
public int Substract(int a, int b) { return a - b; }
public int Calculate()
{
// Raise the event by using the () operator.
return BinaryEvent(1, 2); // 只能在定義事件的類的內部調用,如果寫在外面會編譯不過
}
}
class Program
{
static void Main()
{
SimpleMath sm = new SimpleMath();
// sm.BinaryEvent(1, 2); 編譯錯誤!只能在定義事件的類的內部調用
// 下面兩種註冊方法效果是一樣的,相當於註冊了兩遍,也的確會依序執行兩遍
sm.BinaryEvent += new SimpleMath.BinaryOp(sm.Add);
sm.BinaryEvent += sm.Add;
Console.WriteLine(sm.Calculate()); // 結果是3
// 下面兩種註冊方法效果是一樣的,相當於註冊了兩遍,也的確會依序執行兩遍
sm.BinaryEvent += new SimpleMath.BinaryOp(sm.Substract);
sm.BinaryEvent += sm.Substract;
Console.WriteLine(sm.Calculate()); // -1, 只保留最後一次調用的傳回值(3,3,-1,-1)
}
}
}
(*) 匿名方法
匿名方法的作用是使代碼更簡化,方便了程式員。如果沒有匿名方法,則定義一套事件監聽需要這樣:
class Program
{
public delegate void SomeDelegate(); // 定義委託
static void Main()
{
SomeType obj = new SomeType();
obj.SomeEvent += new SomeDelegate(MyEventHandler);
}
// 一般來說,定義的MyEventHandler方法只用來響應事件,只在上面那一處地方使用
public static void MyEventHandler() // 定義委託所調用的方法
{}
}
上面的代碼比較囉嗦,尤其是為委託和它所調用的方法起名字比較費勁我覺得。有了匿名方法以後,僅需要這樣:
class Program
{
static void Main()
{
SomeType obj = new SomeType();
obj.SomeEvent += delegate{
// 實現事件處理邏輯
}; // 注意要有分號
}
}
與上一段代碼相比,省去了SomeDelegate和MyEventHandler的定義。