References "C # technology revealed" http://www.microsoft.com/china/msdn/events/featureevents/default.mspx Here are some Microsoft .NET programming video data for C / C , delegate It is equivalent to the function pointer, look at the following code: Class Person {PUBLIC PERSON (STRING NAME) {this.name = name;} // Some attributes and methods String name; public void eat (food food);} Person class has one EAT method, for its instance, Zhang San, Li Si, Wang Wu has their own EAT method Person ZHANSAN, LISI, WANGWU; ZHANSAN = New Person ("Zhang San"); Lisi = New Person ("Li "); Wangwu = new Person (" Wang 5 "); we can call EAT method PUBLIC VOID DELEGATE EAT (Food Food) by defining a commission; if you want to call zhansan.eat (food) Eat zhansaneat = new Eat ZHANSAN.EAT); other Similar: Eat Lisierat = New Eat (Lisi.eat); Eat Wangwu = New Eat (WANGWU.EAT); this call zhansaneat (Food) is equivalent to calling zhansan.eat (food) to entrust the most useful It is a commission chain. If Zhang San, Li Si, Wang Wu jointly eats, can define the commissioned 如 定 e e e e e e 中 中 中 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法// Take the method from the entrusted chain to remove togetHereat = zhansaneat Lisieat WANGWueat; // Zhang San, Li Si, Wang Wuyi eats watermelon togethereat (watermelon); // Do not bring Zhang San, only Li Si and Wang Wu eats togethereat = lisieat wangwueat; togethereat; The event mechanism in .NET is implemented by commission.
Here is a source code that C # technology reveals secret, I added some comments, explaining the implementation mechanism of the event // InventoryManager class for updating the stock, it also defines the event that should trigger when the inventory is updated, That is to say that it releases a delegate by the subscriber to call // inventorywatcher class to define the subscriber, you can choose whether to add yourself to the publisher's list, to get using system; namespace delegateEvents {// definition EventArgs derived class is used to carry event information class InventoryChangeEventArgs: System.EventArgs {public InventoryChangeEventArgs (string sku, int change) {this.sku = sku; this.change = change;} string sku; public string Sku {get {return SKU;}} int change; public int change;}}} // Publisher class inventoryManager {// declare a delegation, two parameters are essential, the first is the publisher object, the second You must be a class or a derived class EventArgs public delegate void InventoryChangeEventHandler (object source, InventoryChangeEventArgs e); // delegate instance of the event is defined (i.e. delegation chain, for the subscriber to add to their delegation chain public event InventoryChangeEventHandler OnInventoryChangeHandler; / / InventoryManager method for updating inventory public void UpdateInventory (string sku, int change) {if (0 == change) return; // define event parameters instance, passing and sku change information InventoryChangeEventArgs e = new InventoryChangeEventArgs (sku, change ); / / Judgment whether the commissioned chain is Empty, if not, the subscriber subscribe IF (this.oninventoryChangeHandler! = Null) {console.writeline ("[InventoryManager.UpdateInventory ... Raising Event to all subscribers ... / n"); // Call the delegation list the method this.OnInventoryChangeHandler on (this, e);}}} // subscriber class InventoryWatcher {// publisher defined InventoryManager invnetoryManager; public InventoryWatcher (InventoryManager inventoryManager) {Console.WriteLine ( "[InventoryWatcher.InventoryWatcher] Subscribing to InventoryChange Event / n "); this.invnetoryManager = InventoryManager;