asp.net Multicast Delegates or Multicast Delegates in Asp.net

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#



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.



More Post......



Comments

  1. Awesome blog thanks for sharing Searching for a SEO company in Chennai that can bring your brand to the top results page on Google? Look no further - Adhuntt Media, with their team of SEO experts, can make it happen that too for the best value.
    digital marketing company in chennai
    seo service in chennai
    web designing company in chennai
    social media marketing company in chennai

    ReplyDelete
  2. Excellent blog thanks for sharing Pixies beauty Shop is the best place to buy cosmetics in Chennai. With thousands of premium imported brands to choose from, you’ll never run out of lipstick again. And don’t forget about the best offers and value they provide.
    beauty Shop in Chennai

    ReplyDelete

Post a Comment

Popular posts from this blog