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......
- How to use jsTree plugin within Ember, Example
- Private variables and functions in JavaScript
- Which One is Better for Professional web site Face...
- Top Latest Post Related To the Asp.net And C# Prog...
- Brightness Control Not Working In Windows 10, How ...
- How to protect server from Brute Force Attacks
- Four steps for Pass Microsoft certification exams ...
- What is Namespace in C#, need of namespace or why ...
- What is Delegates in C#, how to declare and use C#...
- Asp.net C#: what is an indexer in C# how to use in...
- How to create a trigger of Example of AFTER INSERT...
- Sql server: What is trigger in sql how to use
- What is Interface, how to use interface in C#
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us
ReplyDeleteVmware Training in Chennai
Web Designing Training in Chennai
AWS Training in Chennai
Linux Training in Chennai
Microsoft Azure Training in Chennai
ReplyDeleteNice post...! I am eagerly waiting for your new updates...
Embedded System Course Chennai
Embedded Training Institutes in Chennai
Corporate Training in Chennai
Power BI Training in Chennai
Linux Training in Chennai
Tableau Training in Chennai
Pega Training in Chennai
Unix Training in Chennai
Primavera Training in Chennai
Embedded Training in Thiruvanmiyur
Embedded Training in Tambaram
Nice Blog, Very Informative Content,waiting for next update...
ReplyDeleteSAS Training in Chennai
sas training fees
sas course fees
sas training in Thiruvanmiyur
SAS Training in Tambaram
clinical sas training in chennai
Mobile Testing Training in Chennai
QTP Training in Chennai
Hibernate Training in Chennai
DOT NET Training in Chennai
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.
ReplyDeletedigital marketing company in chennai
seo service in chennai
web designing company in chennai
social media marketing company in chennai
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.
ReplyDeletebeauty Shop in Chennai
Thank you so much for your information,its very useful and helpful to me.Keep updating and sharing. Thank you. share more details.
ReplyDeleteAi & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai