Interface declare event and the concrete class implement the interface
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 EventDemo(); 6 Console.ReadLine(); 7 } 8 9 static void EventDemo() 10 { 11 MathClass mc = new MathClass(); 12 mc.Age = 10; 13 mc.ChangedEvent += Mc_ChangedEvent; 14 mc.Age = 20; 15 } 16 17 private static void Mc_ChangedEvent(object sender, ChangedEventArgs e) 18 { 19 Console.WriteLine($"The old value is {e.OldValue},new value is {e.NewValue}"); 20 } 21 } 22 23 interface IMath 24 { 25 void Add(int x, int y); 26 27 int Age { get; set; } 28 29 event EventHandler<ChangedEventArgs> ChangedEvent; 30 } 31 32 public class MathClass : IMath 33 { 34 private int age; 35 public int Age 36 { 37 get 38 { 39 return age; 40 } 41 set 42 { 43 if(value!=age) 44 { 45 int oldValue = age; 46 age = value; 47 int newValue = value; 48 OnChanged(oldValue, newValue); 49 } 50 } 51 } 52 53 public event EventHandler<ChangedEventArgs> ChangedEvent; 54 55 protected virtual void OnChanged(int oldValue,int newValue) 56 { 57 var handler = ChangedEvent; 58 handler?.Invoke(this, new ChangedEventArgs(oldValue, newValue)); 59 } 60 61 public void Add(int x, int y) 62 { 63 Console.WriteLine($"x+y={x + y}"); 64 } 65 } 66 67 public class ChangedEventArgs:EventArgs 68 { 69 public int OldValue { get; set; } 70 public int NewValue { get; set; } 71 72 public ChangedEventArgs(int oldValue,int newValue) 73 { 74 OldValue = oldValue; 75 NewValue = newValue; 76 } 77 }