Asp.net programming we consider all important topic for knowledge about controls,web Services, JSON, Ajax, database, and interview related Question. In this Asp.net and sql server tutorial blog we give many examples of jquery as like How Play YouTube Video in Your Asp.net Website by URL, How to Play YouTube Videos in Asp.net by using Jquery , Jquery Change div background on mouseover in asp.net , JQuery UI Datepicker (Calendar) etc.

Friday, September 2, 2016

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



3 comments:

  1. After looking into a handful of the blog articles on your site, I really like your technique of writing a blog. I book marked it to my bookmark site list and will be checking back in the near future. Take a look at my website as well and let me know your opinion.
    Car Wash Services in Mumbai

    ReplyDelete