Part 36 to 39 Talking about Delegates in c #,

Source: Internet
Author: User

Part 36 to 39 Talking about Delegates in c #,

Part 36 Delegates in c #

Class Progim {public static void Main () {List <Employee> empList = new List <Employee> (); empList. add (new Employee () {ID = 101, Name = "Mary", Salary = 5000, Experience = 5}); empList. add (new Employee () {ID = 102, Name = "Mike", Salary = 4000, Experience = 4}); empList. add (new Employee () {ID = 103, Name = "John", Salary = 6000, Experience = 6}); empList. add (new Employee () {ID = 104, Name = "Todd", Salary = 3000, Experience = 3}); Employee. promoteEmployee (empList) ;}} class Employee {public int ID {get; set;} public string Name {get; set ;} public int Salary {get; set ;} public int Experience {get; set;} public static void PromoteEmployee (List <Employee> employeeList) {foreach (Employee employee in employeeList) {if (employee. experience> = 5) // this is hard code logic, next part we will using delegete replace it. console. writeLine (employee. name + "Promoted ");}}}View Code

Part 38 Delegates usage in c # contiued

Class Progim {public static void Main () {List <Employee> empList = new List <Employee> (); empList. add (new Employee () {ID = 101, Name = "Mary", Salary = 5000, Experience = 5}); empList. add (new Employee () {ID = 102, Name = "Mike", Salary = 4000, Experience = 4}); empList. add (new Employee () {ID = 103, Name = "John", Salary = 6000, Experience = 6}); empList. add (new Employee () {ID = 104, Name = "Todd", Salary = 3000, Experience = 3}); IsPromotable isPromotable = new IsPromotable (Promote); Employee. promoteEmployee (empList, isPromotable); // Employee. promoteEmployee (empList, emp => emp. experience> 5);} // it unnecessary to use this method, you can use lambda expression to replace it. public static bool Promote (Employee emp) {if (emp. experience> 5) return true; else return false ;}} delegate bool IsPromotable (Employee empl); class Employee {public int ID {get; set;} public string Name {get; set;} public int Salary {get; set;} public int Experience {get; set;} public static void PromoteEmployee (List <Employee> employeeList, IsPromotable IsEligibleToPromote) {foreach (Employee in employeeList) {if (IsEligibleToPromote (employee) // this is hard code logic, next part we will using delegete replace it. console. writeLine (employee. name + "Promoted ");}}}View Code

Part 39 Multicast Delegates in C #

A Multicast delegate is a delegate that has references to more than one function. When you invoke a multicast delegate, all the function the delegate is pointing to, are invoked.

There are 2 approaches to create a multicast delegate. Depending on the approach you use + or + = to register a method with delegate

-Or-= to un-register a method with the delegate

Note: A multicast delegate, invokes the methods in the invocation list, in the same order in which they are added.

If the delegate has a return type other than void and if the delegate is a multicast delegate, only the value of the last invoked method will be returned. along the same lines, if the delegate has an out parameter, the value of the output parameter, will be the value assigned by the last method.

Common interview question-where do you use multicast delegates?

Multicast delegate makes implementation of observer (observer) design pattem (pattern) very simple. observer pattem is also called as publish/subscribe (subscription) pattem.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.