C# 委托 事件 匿名方法
(*) 委托 delegate
從最簡單的例子開始:
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)); Console.ReadLine(); } } }
輸出結果為:20
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(); Console.ReadLine(); } }
輸出結果為:A
B(*) 事件
委托是個類型,而事件是個成員。看下麵的代碼:
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) Console.ReadLine(); } } }
輸出結果為:3
-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{ // 實現事件處理邏輯 }; // 注意要有分號 } }
拓展:點擊打開鏈接
最後更新:2017-04-03 12:54:12