Thursday, November 26, 2015

Multicast Delegates in C#

In today's article we will be discussing multicast delegates in C# with example.

Multicast Delegates

As we discussed last week about delegates here, a delegate points to a function. We saw an example of a delegate pointing to a function and how we can use it. However, when using delegates we are not limited to pointing the delegate instance to only one method. With one delegate call, we can invoke multiple methods. The multicast delegate keeps a list of assigned delegates and calls them one by one when invoked.

multicast delegates in C#
Multicast Delegates in C#


Let's look at an example.
Suppose, we have an Employee class like this:

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime PositionChangeDate { get; set; }
    public decimal Salary { get; set;}
}

And here is the multicast delegate for employee promotion:

public delegate void CustomDel(Employee employee);
static void Main(string[] args)
{
       var employees = new List<Employee>()
       {
             new Employee(){Id = 1, Name = "Abhi", Salary = 10},
             new Employee(){Id = 2, Name = "John", Salary = 20}
        };

        CustomDel SalaryRaiseDel, PositionDateDel, 
                  EmployeePromoteMulticastDelegate;
        SalaryRaiseDel = new CustomDel(EmployeeSalaryRaise);
        PositionDateDel = new CustomDel(PostionDateUpdate);
      
        //multiple delegates assigned
        EmployeePromoteMulticastDelegate = PositionDateDel 
                                         + SalaryRaiseDel;

        foreach (var emp in employees)
        {
            EmployeePromoteMulticastDelegate(emp);
            {
                Console.WriteLine("Id = " + emp.Id);
                Console.WriteLine("Name = " + emp.Name);
                Console.WriteLine("Salary = " + emp.Salary);
            }
        }

        Console.ReadLine();
   }
    

//set position change date to current time
private static void PostionDateUpdate(Employee employee)
{
     employee.PositionChangeDate = DateTime.Now;
}

//raise employee salary by 10%
private static void EmployeeSalaryRaise(Employee employee)
{
     employee.Salary += employee.Salary + employee.Salary 
          * (decimal) 0.1;
}

So here I created a multicast delegate called EmployeePromoteMulticastDelegate. This delegate is assigned 2 other delegates viz. SalaryRaiseDel & PositionDateDel. So when we call the EmployeePromoteMulticastDelegate with Employee parameter, it calls EmployeeSalaryRaise() and PostionDateUpdate() methods. So with one call we were able to call 2 methods.

Any delegate when assigned multiple methods to invoke becomes multicast delegate. There is no special syntax for multicast delegates.

Conclusion

Multicast delegates are a powerful feature of C# and are used to apply publish subscribe pattern. By invoking an event, we can call multiple methods who have subscribed to the event.

For future updates to my weekly blog, please subscribe to my blog via the "Subscribe To Weekly Post" feature at the right and follow me on Twitter. Until then Happy Coding :)


No comments:

Post a Comment