Reference
"C # technology reveals"
Http://www.microsoft.com/china/msdn/events/featureevents/default.mspx here has some Microsoft .NET programming video data
For those who have learned C / C , Delegate is equivalent to the function pointer and looks at the following code:
Class Person
{
Public Person (String Name)
{
THIS.NAME = Name;
}
// Some properties and methods
String name;
Public void Eat (Food Food);
}
The Person class has an EAT method. For example, Zhang San, Li Si, Wang Wu has their own EAT method
Person ZHANSAN, LISI, WANGWU
ZHANSAN = New Person ("Zhang 3");
Lisi = New Person ("Li Si");
WANGWU = New Person ("Wang 5");
We can call EAT methods by defining a delegate
Public void delegate Eat (Food Food);
If you want to call ZHANSAN.EAT (FOOD)
Eat zhansaneat = new Eat (zhansan.eat);
Similar:
Eat Lisierat = New Eat (Lisi.eat);
Eat Wangwu = New Eat (Wangwu.eat);
This is called ZHANSANEAT (FOOD) is equivalent to calling zhansan.eat (food)
The most useful is the entrusted chain, if Zhang San, Li Si, Wang Wu together
It can be defined as follows
// Define composite commission
Eat togethereat;
// C # passes the " " through " " to delegate, will be entrusted to the entrusted chain
// Remove the method from the delegate chain by "-"
TOGETHEREAT = ZHANSANEAT LISIEAT WANGWUEAT;
// Zhang San, Li Si, Wang Wuyi eats watermelon
TogetHeat; Watermelon);
// Do not take Zhang San, only Li Si and Wang Wu eat
TOGETHEREAT = Lisierat WANGWueat;
TogetHeat; Watermelon);
The event mechanism in .NET is implemented by delegate.
Here is a source code that C # technology reveals secret, I added some comments to explain the implementation mechanism of the event.
// InventoryManager class is used to update the inventory, which also defines the event that should trigger when the inventory is updated, that is, it releases a delegate by the subscriber call // InventoryWatcher class to define the subscriber, you can choose whether to use yourself Add to the publisher's list to get using system when updating your stock;
EventArgs namespace DelegateEvents {// definition of 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 {Return Sku;}} int change; public int change;}}} // Publisher class inventoryManager {// declare a delegation, two parameters are essential, the first is released Objects, the second must be an Eventargs class or its party, a classEventhandler (Object Source, InventoryChangeEventAndler (Object Source, InventoryChangeEventAndler (//); // Define the entrusted event instance (ie the commission chain, used to subscribe to add yourself to the entrusted 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); // delegate determines whether the list is empty, if not empty, indicating a subscriber subscribes if (! this.OnInventoryChangeHandler = null) {Console.WriteLine ( "[InventoryManager.UpdateInventory] Raising event to all Subscribers ... / n "); // Call on the delegation list The method this.OnInventoryChangeHandler (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; // Connect to entrust themselves to InventoryManager.InventoryChangeEventHandler inventoryManager.OnInventoryChangeHandler = new InventoryManager.InventoryChangeEventHandler (OnInventoryChange);} // method subscribers for a publisher inventory update Call Void OnNventoryChange (Object Source, InventoryChangeEventArgs E) {INT CHANGE =
E.Change; console.writeline ("[inventoryManager.oninventoryChange] / n / tpart '{0}' WAS {1} by {2} Units / N", E.SKU, CHANGE> 0? "increased": "DecReased ", Math.Abs (E.Change));}} ///
The above is just a little understanding, many places are immature, and may be inappropriate, welcome points, and learn together.