Events&Delegates

Events & Delegates in C#

Tuba Mansoor
Latest posts by Tuba Mansoor (see all)

Events & Delegates

Events and Delegates in C# are no doubt one of the most confusing topics.

Let us explore more about them. Code used in this article can be downloaded from GitHub.

Delegates

Delegates are function pointers.

Let’s write some code to understand delegates.

I have created a sample console application. When a user registers, an email and sms verification is sent.

 class Program
        {
            static void Main()
            {
                var registerUser = new RegisterUser();
                var emailVerification = new EmailVerification();
                var smsVerification = new SMSVerification();

                registerUser.registerUserDelegateInstance += emailVerification.OnUserRegistered;
                registerUser.registerUserDelegateInstance += smsVerification.OnUserRegistered;
                registerUser.RegisterAUser();// Call delegate

                Console.ReadLine();
            }

        }
        public class RegisterUser
        {
            public delegate void registerUserDelegate(); // declare a delegate
            public registerUserDelegate registerUserDelegateInstance; // create a delegate instance

            public void RegisterAUser()
            {
                Console.WriteLine("User registered");
                if (registerUserDelegateInstance != null)
                {
                    registerUserDelegateInstance();// Call the delegate
                }
            }

        }
        public class EmailVerification
        {

            public void OnUserRegistered()
            {
                Console.WriteLine("Sent Email for Verification");
            }
        }

        public class SMSVerification
        {

            public void OnUserRegistered()
            {
                Console.WriteLine("Sent SMS for Verification");
            }
        }
Events & delegates
Events & delegates

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).

See also  C# Collections - Behind the Scene: A Must for Every C# Programmers

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:

Events & delegates

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 want 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;
Events & delegates

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.

See also  Abstraction vs Encapsulation in OOPS

I have modified the above code as below:

  class Program
    {

        static void 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();
        }

    }


    public class RegisterUser
    {
        public delegate void registerUserEventHandler(object source, EventArgs Args); //define a delegate
        public event registerUserEventHandler registerUserEvent; // define an event

        public void RegisterAUser()
        {
            Console.WriteLine("User registered");
            if (registerUserEvent != null)
            {
                registerUserEvent(this, EventArgs.Empty);// call event
            }
        }

   }
    public class EmailVerification
    {

        public void OnUserRegistered(object source, EventArgs e)
        {
            Console.WriteLine("Sent Email for Verification");
        }
    }

    public class SMSVerification
    {

        public void OnUserRegistered(object source, EventArgs e)
        {
            Console.WriteLine("Sent SMS for Verification");
        }
    }
Events & delegates
Events & delegates

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 a 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.

Output is as below:

Events & delegates

So the summarised steps are:

  1. Declare a delegate (registerUserEventHandler)
  2. Declare an event based on that delegate (registerUserEvent)
  3. Create an event (registerUserEvent(this, EventArgs.Empty);)
  4. Subscribe methods to that event(registerUser.registerUserEvent += emailVerification.OnUserRegistered;)
  5. Fire that event (RegisterAUser)
See also  Cloud Computing : How to deploy an app on IBM Cloud using Cloud Foundry

Every time you declare an event, you do not have to declare a delegate too. Dotnet provides an in built delegate called EventHandler which can be used directly while calling an event as below:

public event EventHandler registerUserEvent;
 class Program
    {

        static void 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();
        }

    }


    public class RegisterUser
    {
        public event EventHandler registerUserEvent;
        public void RegisterAUser()
        {
            Console.WriteLine("User registered");
            if (registerUserEvent != null)
            {
                registerUserEvent(this, EventArgs.Empty);// call event
            }
        }

    }
    public class EmailVerification
    {

        public void OnUserRegistered(object source, EventArgs e)
        {
            Console.WriteLine("Sent Email for Verification");
        }
    }

    public class SMSVerification
    {

        public void OnUserRegistered(object source, EventArgs e)
        {
            Console.WriteLine("Sent SMS for Verification");
        }
    }
Events & delegates

But if we try to make the event null, it won’t let us:

Events & delegates

So, events are encapsulated delegates that provide an extra layer of security. This is the reason why we prefer using events over delegates.

I hope this article brings you one step closer to understanding events and delegates.

Here is a great video to understand the same.

https://csharpindepth.com/articles/Events

https://docs.microsoft.com/en-us/dotnet/csharp/delegates-events