protected virtual void OnPriceChanged(PriceChangedEventArgs e) { if (PriceChanged != null) { PriceChanged(this, e); } }
static void stock_PriceChanged(object sender, PriceChangedEventArgs e) { System.Console.WriteLine($"Price changed: Old: {e.LastPrice} New: {e.NewPrice}"); if ((e.NewPrice - e.LastPrice) / e.LastPrice > 0.1M) { System.Console.WriteLine("Price increased 10%!"); } }
public event EventHandler <PriceChangedEventArgs> PriceChanged; // 2 & 3 Use Framework define generic delegate protected virtual void OnPriceChanged(PriceChangedEventArgs e) // Protected Virtual Method that fires the event { PriceChanged?.Invoke(this, e); }
static void stock_PriceChanged(object sender, PriceChangedEventArgs e) { if ((e.NewPrice - e.LastPrice) / e.LastPrice > 0.1M) { Console.WriteLine("Alert, 10% stock price increased!"); } }