• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Part36to39TalkingaboutDelegatesinc#

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Part 36 Delegates in c#

Part 37 Delegates usage 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 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(观察者) design pattem(模式) very simple. observer pattem is also called as publish/subscribe(订阅) pattem.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#EntityFrameworkwithMSSQL,MYSQL发布时间:2022-07-13
下一篇:
WinCE程序C/C++/C#实现带时间标记的日志记录发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap