Standard event mode and standard Event Mode
From http://www.cnblogs.com/willick/p/4102174.html, only for reference
The. NET Framework defines a standard mode for event programming to ensure consistency between the. NET Framework and user code. System. EventArgs is the core of the standard mode. It is a base class without any members for passing event parameters. First, define EventArgs:
1 public class PriceChangeEventArgs : System.EventArgs {2 public readonly decimal oldPrice;3 public readonly decimal newPrice;4 public PriceChangeEventArgs(decimal oldPrice, decimal newPrice) {5 6 this.oldPrice = oldPrice;7 this.newPrice = newPrice;8 }9 }
Then define the delegate for the event. The following conditions must be met:
- It must be of the void return type;
- There must be two parameters, the first is the object type, and the second is the EventArgs type (subclass );
- Its name must end with EventHandler.
Considering that it is difficult to define your own delegate for each event, the. NET Framework predefines a general delegate System. EventHandler <TEventArgs> for us:
public delegate void EventHandler<TEventArgs> (object source, TEventArgs e) where TEventArgs : EventArgs;
If you do not use the EventHandler <TEventArgs> of the framework, you need to define one:
public delegate void PriceChangedEventHandler (object sender, PriceChangedEventArgs e);
If no parameter is required, you can directly use EventHandler (<TEventArgs> is not required ). With EventHandler <TEventArgs>, we can define the event in the example as follows:
public class IPhone6 { ... public event EventHandler<PriceChangedEventArgs> PriceChanged; ...}
Finally, in the event standard mode, you also need to write a protected virtual method to trigger the event. This method must be prefixed with On, and the event name (PriceChanged) must be added, and an EventArgs parameter must be accepted, as follows:
1 public class IPhone6 {2 ...3 public event EventHandler<PriceChangedEventArgs> PriceChanged;4 protected virtual void OnPriceChanged(PriceChangedEventArgs e) {5 if (PriceChanged != null) PriceChanged(this, e);6 }7 ...8 }
The following is a complete example:
Public class PriceChangedEventArgs: System. eventArgs {public readonly decimal OldPrice; public readonly decimal NewPrice; public PriceChangedEventArgs (decimal oldPrice, decimal newPrice) {OldPrice = oldPrice; NewPrice = newPrice;} public class IPhone6 {decimal price; public event EventHandler <PriceChangedEventArgs> PriceChanged; protected virtual void OnPriceChanged (PriceChangedEventArgs e ){ If (PriceChanged! = Null) PriceChanged (this, e);} public decimal Price {get {return price;} set {if (price = value) return; decimal oldPrice = price; price = value; // triggered if the call list is not empty. If (PriceChanged! = Null) OnPriceChanged (new PriceChangedEventArgs (oldPrice, price) ;}} class Program {static void Main () {IPhone6 iphone6 = new IPhone6 () {Price = 5288 M }; // subscribe to the event iphone6.PriceChanged + = iphone6_PriceChanged; // adjust the price (event occurs) iphone6.Price = 3999; Console. readKey ();} static void iphone6_PriceChanged (object sender, PriceChangedEventArgs e) {Console. writeLine ("year-end promotions, iPhone 6 for sale only" + e. newPrice +" Yuan, original price "+ e. OldPrice +" Yuan, come and grab it! ");}}