Please enable JavaScript.
Coggle requires JavaScript to display documents.
Events - Coggle Diagram
Events
-
Rather creating our own delegate we should use inbult delegate EventHandler. public event EventHandler<PriceReadEventArgs>? PriceChanged
PriceReadEventArgs nothing but derived from EventArgs. Ex: PriceReadEventArgs:EventArgs{ public decimal price}
EventName.Invoke(this,new PriceReadEventArgs(price))
Events are the .NET implementation of observer design pattern . They enable an object to notify other objects when something happens
-
-
when something happens at the 1).party (an event is triggered), the 2).host (event) sends messengers (delegates) to let everyone know. People who want to know about it (subscribers) listen for these messengers and react accordingly. This way, the party goes smoothly without the host needing to talk to every guest individually
If you were to pass messages without using delegates in an event-like scenario, you would likely end up with a tightly coupled and less organized system. Subscribers would need to directly reference or somehow know about the source of the messages.
When you declare an event using the event keyword, you're actually encapsulating a delegate within that event. This encapsulation restricts direct access to the delegate from outside the class, promoting a more controlled and organized way of managing subscriptions and invocations.
The encapsulation provided by events ensures that subscribers can only interact with the event through well-defined subscription and invocation mechanisms, preventing unintended modifications to the underlying delegate. This makes the code more maintainable and minimizes unexpected behavior.
-
-