Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we need events for callback?

Tags:

c#

.net

We can call callback methods using delegates. For eg,

public delegate bool ContinueProcessing();

// later in code we can write ,

ContinueProcessing cp = new ContinueProcessing(IsDataAvailable);

cp += new ContinueProcessing(IsTransactionComplete);

//later in code defination of methods

bool IsDataAvailable() { return true; }

bool IsTransactionComplete() { return true; }

cp.Invoke() ;

The above call will call two boolean method one after the other. Why we need "Events" ? What is the purpose of "Events" ?

like image 599
Ashish Avatar asked Apr 19 '26 04:04

Ashish


1 Answers

Events are callbacks where you can have multiple subscribers who don't interfere with each other and can't call each other.

Delegates provide the encapsulation of "this is the action I want to take" and events provide the encapsulation of the pub/sub model.

See my article on events for more information.

like image 56
Jon Skeet Avatar answered Apr 21 '26 19:04

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!