Many people are initially confused with the real need for delegates and events. I was one of them, and it took me a while to figure this out :-). I recently answered a similar request in ASP.NET forums and thought it would be nice if I created a blog post on this topic! Here is the request:
“I read an example somewhere in the bank class that if the minimum balance is reached, you need to tell the rest of the application that the mines have been reached, but we can’t do this simply by calling the usual method. For example: say, when we subtract some amount out of balance, and if the minimum is reached, then call some method to take some action, I absolutely don’t understand why we need delegates and user events here? "
The thing in the Bank case, you can definitely call a method, but then it would be just procedural programming, we need event-based programming when we want our code to respond to some events generated by the system.
For example: think that Windows is a system, and we write code (in any language) where we want to capture an event, such as mouse_click (). Now how does our program know that a mouse click has occurred? We can use low-level code for it, but since the OS is already processing low-level code, it is best to capture the event raised by the OS.
In other words, the moment mouse_click () occurs, the OS fires an event. The OS does not care about who captures this event and uses it, it just sends a notification. Then any code (for example, ours) can capture this event and use it accordingly. This saves us a lot of time to write code for the same. And other programs can also use the same event and process it accordingly.
Similarly, the banking system can be huge and many other external applications can access it. The banking system does not know how many such applications are in it, or depends on it, and how they will handle certain situations, for example, when the balance is low, so it just fires an event when a low balance occurs, and this event can be used by any other code, in addition to the bank code itself.
Note that each susbcriber for this event can handle this event independently, for example. the bank code may stop something from being executed if the balance is low, some other reporting application may send an email in this case, or some ATM code may stop the transaction of a particular participant and inform the user that the balance is low.
Hope this clears a bit!