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 = , Name = "Mary", Salary = , Experience = });
empList.Add(new Employee() { ID = , Name = "Mike", Salary = , Experience = });
empList.Add(new Employee() { ID = , Name = "John", Salary = , Experience = });
empList.Add(new Employee() { ID = , Name = "Todd", Salary = , Experience = });
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 >= ) //this is hard code logic, next part we will using delegete replace it.
Console.WriteLine(employee.Name + "Promoted");
}
}
}

Part 38 Delegates usage in c# contiued

 class Progim
{
public static void Main()
{
List<Employee> empList = new List<Employee>();
empList.Add(new Employee() { ID = , Name = "Mary", Salary = , Experience = });
empList.Add(new Employee() { ID = , Name = "Mike", Salary = , Experience = });
empList.Add(new Employee() { ID = , Name = "John", Salary = , Experience = });
empList.Add(new Employee() { ID = , Name = "Todd", Salary = , Experience = });
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 > )
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");
}
}
}

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.

Part 36 to 39 Talking about Delegates in c#的更多相关文章

  1. dicom网络通讯入门(1)

    看标准 越看越糊,根本原因:dicom抽象得非常严重,是“专家”弄的.没办法. 又是什么服务类 又是什么sop,相信你把dicom标准看到头大 都不知如何下手. 不就是 socket么 这有何难. 首 ...

  2. 不重新编译php安装配置eAccelerator

    eAccelerator属于一个免费的开源php加速.优化.编译和动态缓存项目,原理和apc类似,都是通过缓存php编译后的opcode代码来提高php脚本的执行性能,而且eAccelerator本身 ...

  3. jquery中ajax用return来返回值无效

    jquery中,ajax返回值,有三种写法,只有其中一种是成功的 /** * async:false,同步调用 * 返回1:2 * 失败 * 分析:ajax内部是一个或多个定义的函数,ajax中ret ...

  4. 获取文本的编码类型(from logparse)

    import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.F ...

  5. PHP两种基础的算法:冒泡、快速排序法》》》望能够帮助到大家

    首先扯个淡@@@@@@@@@ 算法是程序的核心,一个程序的好坏关键是这个程序算法的优劣. 冒泡排序法 原理:在要排序的一组数中,对当前还未排好的序列,从前往后对相邻的两个数依次进行比较和调整,让较大的 ...

  6. Java 8 Stream API详解--转

    原文地址:http://blog.csdn.net/chszs/article/details/47038607 Java 8 Stream API详解 一.Stream API介绍 Java8引入了 ...

  7. python笔记1:python函数的使用

    一.函数:len() 1:作用:返回字符串.列表.字典.元组等长度 2:语法:len(str) 3:参数: str:要计算的字符串.列表.字典.元组等 4:返回值:字符串.列表.字典.元组等元素的长度 ...

  8. PHP实现四种基本排序算法

    前提:分别用冒泡排序法,快速排序法,选择排序法,插入排序法将下面数组中的值按照从小到大的顺序进行排序. $arr(1,43,54,62,21,66,32,78,36,76,39); 1. 冒泡排序 思 ...

  9. php 冒泡 快速 选择 插入算法 四种基本算法

    php四种基础算法:冒泡,选择,插入和快速排序法 来源:PHP100中文网 | 时间:2013-10-29 15:24:57 | 阅读数:120854 [导读] 许多人都说 算法是程序的核心,一个程序 ...

随机推荐

  1. Active Low-Pass Filter Design 低通滤波器设计

    2nd order RC Low-pass Filter Center frequency    fc = 23405.13869[Hz] Q factor                  Q = ...

  2. ThinkPHP模板(一)

    如何关闭ThinkPHP的模板缓存 ThinkPHP的模板缓存是无奈关闭的,因为内置的模板引擎是一个编译型的模板引擎,必须经过编译后生成一个可执行的缓存文件才能被执行.但是可以设置缓存的有效期,例如设 ...

  3. System.Runtime.InteropServices.COMException (0x800706BA) 解决方法

    提示“操作失败:无法获取MAC地址.”错误的解决方法. .NET 获取 MAC地址可能会遇到   System.Runtime.InteropServices.COMException (0x8007 ...

  4. git status对应中文名称

    git status显示出来的中文文档是一串字符码,不能正常识别 git config --global core.quotepath false 这一句代码就可以帮你

  5. C++ for循环效率

    1.考虑二维数组,在C++中,以先行后列的方式存储连续的数组元素.也就是同一行的元素在一起,同一列的元素之间有间隔,且间隔相同.理想情况下,二维数组的元素是随机访问的,可以直接定位,即i*列数+j.因 ...

  6. 【转】SoapUI5.0创建WebService接口模拟服务端

    原文:http://blog.csdn.net/a19881029/article/details/26348627 使用SoapUI创建WebService接口模拟服务端需要接口描述文件 MathU ...

  7. iis7负载均衡

    Windows平台分布式架构实践 - 负载均衡(下)   Windows平台分布式架构实践 - 负载均衡   Windows平台分布式架构实践 - 负载均衡   概述 最近.NET的世界开始闹腾了,微 ...

  8. 文件系统缓存dirty_ratio与dirty_background_ratio两个参数区别

    这两天在调优数据库性能的过程中需要降低操作系统文件Cache对数据库性能的影响,故调研了一些降低文件系统缓存大小的方法,其中一种是通过修改/proc/sys/vm/dirty_background_r ...

  9. [原创]Microsoft.AnalysisServices.ManagementDialogs.ServerPropertiesLanguagePanel 的类型初始值设定项引发异常

    问题: 安装SQL SERVER 2012 SP1后,有可能在右键AS服务器想打开属性面板时候会出现如下错误:   解决: 这个需要安装相应的热修复补丁470544 相应文章: http://smal ...

  10. Docker大行其道—镜像

    导读 作为Docker三大核心概念之一,Docker镜像(Docker Image)是一个面向Docker引擎的只读模板,包含文件系统.实际上每个Docker镜像包含一个独立的运行环境,如一个镜像包含 ...