Well you see the events and delegates are two different things. So don't confuse them with one preference over other. I am not satisfied with this article. Please correct it.
Absolutely Madhukar, events & delegates are two different things! But in this scenario (event subscription), events are preferred over delegates as they provide an extra layer of encapsulation. Here is an excellent resource to understand events & delegates : https://csharpindepth.com/articles/Events
Events and Delegates in C# are undoubtedly among the most confusing topics. Let us explore more about these. The code used in this article can be downloaded from GitHub.
Delegates
Delegates are function pointers. Let’s write some code using delegates to understand delegates.
I have created a sample console application. When a user registers, an email and SMS verification is sent.
publicdelegatevoid registerUserDelegate(); // declare a delegate
public registerUserDelegate registerUserDelegateInstance; // create a delegate instance
publicvoid RegisterAUser() {
Console.WriteLine("User registered");
if (registerUserDelegateInstance != null) {
registerUserDelegateInstance(); // Call the delegate
}
}
}
publicclass EmailVerification {
publicvoid OnUserRegistered() {
Console.WriteLine("Sent Email for Verification");
}
}
publicclass SMSVerification {
publicvoid OnUserRegistered() {
Console.WriteLine("Sent SMS for Verification");
}
}
In the above code, we have initially defined a delegate signature (registerUserDelegate). Any method that accepts no parameters and returns void can be pointed by this delegate, i.e., the method this delegate points to should have the same method signature (OnUserRegistered).
We have created a delegate instance ‘registerUserDelegateInstance‘ which points to OnUserRegistered methods of EmailVerification & SMSVerification classes. When a delegate instance is called in the RegisterAUser method, all the methods it points to are executed.
If we run the application, we get an output as below.
Advantages of using Delegates
LINQ uses Func & Action delegates.
Events are nothing but encapsulated delegates. (More on this later)
Methods cannot be passed as parameters to other methods. But delegates can be passed as method parameters.
Delegate help us reduce coupling. In the future, if we want an address verification, we simply have to add a new class for address verification and point a method of this class to the delegate instance. If we had not used delegates here, we would have simply called the email verification & SMS verification methods in RegisterAUser method. In this case, every time we add a new verification, RegisterUser class gets modified increasing coupling.
Disadvantages of using Delegates
You can make a delegate instance null as below.
registerUser.registerUserDelegateInstance = null;
Now, the output will be as below.
The email verification and SMS verification methods are not called as the delegate becomes null.
Let’s have a look at how events help us solve this issue.
Events
Let’s have a look at how we can use events help provide delegate encapsulation.
I have modified the above code as below.
class Program {
staticvoid Main() {
var registerUser = new RegisterUser();
var emailVerification = new EmailVerification();
var smsVerification = new SMSVerification();
registerUser.registerUserEvent += emailVerification.OnUserRegistered; //subscribe to an event
registerUser.registerUserEvent += smsVerification.OnUserRegistered; //subscribe to an event
registerUser.RegisterAUser(); // publisher
Console.ReadLine();
}
}
publicclass RegisterUser {
publicdelegatevoid registerUserEventHandler(object source, EventArgs Args); //define a delegate
publicevent registerUserEventHandler registerUserEvent; // define an event
I have a class ‘RegisterUser’. This class has delegate ‘registerUserEventHandler’. Then, I have created an event based on that delegate called ‘registerUserEvent’. When a user registers, the RegisterAUser method calls the event that we had declared earlier. So what is happening here is that when a user registers, it calls an event.
I have then created an EmailVerification which will contain a method ‘OnUserRegistered’ that will receive parameters sent by an event.
In our Main class, the EmailVerification & SMSVerification will subscribe to the event.
Let’s run the code.
The output is as below.
So, the summarised steps are,
Declare a delegate (registerUserEventHandler)
Declare an event based on that delegate (registerUserEvent)
Create an event (registerUserEvent(this, EventArgs.Empty);)
Subscribe methods to that event(registerUser.registerUserEvent += emailVerification.OnUserRegistered;)
Fire that event (RegisterAUser)
Every time you declare an event, you do not have to declare a delegate too. Dotnet provides an inbuilt delegate called EventHandler which can be used directly while calling an event as below.
publicevent EventHandler registerUserEvent;
class Program
{
staticvoid Main()
{
var registerUser = new RegisterUser();
var emailVerification = new EmailVerification();
var smsVerification = new SMSVerification();
registerUser.registerUserEvent += emailVerification.OnUserRegistered; //subscribe to an event
registerUser.registerUserEvent += smsVerification.OnUserRegistered; //subscribe to an event
Madhukar KrishnaPosted Apr 26, 2019, 7:17 AM
Well you see the events and delegates are two different things. So don't confuse them with one preference over other. I am not satisfied with this article. Please correct it.