static void Main(string[] args) { HereIsEventSource eventSource = new HereIsEventSource(); EventResponder eventResponder = new EventResponder(); eventSource.EventName += eventResponder.Action; }
static void Main(string[] args) { // Step 8: Event sample, 事件的一个样本 // 1. event source: eventSource HereIsEventSource eventSource = new HereIsEventSource(); // 3. Event subscriber: eventSubscriber EventSubcriber eventSubscriber = new EventSubcriber(); // 2. Event: EventName // 5. use “+=” to subscribe the event // SubscriberDoSomthing: action after event triggerred eventSource.EventName += eventSubscriber.SubscriberDoSomthing; eventSource.EventSourceAction4TriggerEvent(); }
public void Action(HereIsEventSource eventSource, EventNameEventArgs e) { System.Console.WriteLine("do something!"); }
// Due to subscribe event via delegate, so the method's signature must same as eventhandler delegate // - eventSource tells EventSubscriber do for which Event Source // - e tells EventSubscriber the action should base on which parameters public void SubscriberDoSomthing(HereIsEventSource eventSource, EventNameEventArgs e) { System.Console.WriteLine($"Event Suberscriber do something after Event source trigger the event!"); System.Console.WriteLine($"Do something with 1st agrs of EventNameEventArgs: {e.FirstArgs} "); System.Console.WriteLine($"Do something with 2nd agrs of EventNameEventArgs: {e.SecondArgs} "); }