Part 36 to 39 Talking about Delegates in c#
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#的更多相关文章
- dicom网络通讯入门(1)
看标准 越看越糊,根本原因:dicom抽象得非常严重,是“专家”弄的.没办法. 又是什么服务类 又是什么sop,相信你把dicom标准看到头大 都不知如何下手. 不就是 socket么 这有何难. 首 ...
- 不重新编译php安装配置eAccelerator
eAccelerator属于一个免费的开源php加速.优化.编译和动态缓存项目,原理和apc类似,都是通过缓存php编译后的opcode代码来提高php脚本的执行性能,而且eAccelerator本身 ...
- jquery中ajax用return来返回值无效
jquery中,ajax返回值,有三种写法,只有其中一种是成功的 /** * async:false,同步调用 * 返回1:2 * 失败 * 分析:ajax内部是一个或多个定义的函数,ajax中ret ...
- 获取文本的编码类型(from logparse)
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.F ...
- PHP两种基础的算法:冒泡、快速排序法》》》望能够帮助到大家
首先扯个淡@@@@@@@@@ 算法是程序的核心,一个程序的好坏关键是这个程序算法的优劣. 冒泡排序法 原理:在要排序的一组数中,对当前还未排好的序列,从前往后对相邻的两个数依次进行比较和调整,让较大的 ...
- Java 8 Stream API详解--转
原文地址:http://blog.csdn.net/chszs/article/details/47038607 Java 8 Stream API详解 一.Stream API介绍 Java8引入了 ...
- python笔记1:python函数的使用
一.函数:len() 1:作用:返回字符串.列表.字典.元组等长度 2:语法:len(str) 3:参数: str:要计算的字符串.列表.字典.元组等 4:返回值:字符串.列表.字典.元组等元素的长度 ...
- PHP实现四种基本排序算法
前提:分别用冒泡排序法,快速排序法,选择排序法,插入排序法将下面数组中的值按照从小到大的顺序进行排序. $arr(1,43,54,62,21,66,32,78,36,76,39); 1. 冒泡排序 思 ...
- php 冒泡 快速 选择 插入算法 四种基本算法
php四种基础算法:冒泡,选择,插入和快速排序法 来源:PHP100中文网 | 时间:2013-10-29 15:24:57 | 阅读数:120854 [导读] 许多人都说 算法是程序的核心,一个程序 ...
随机推荐
- CSS复合样式
关于font OK,我们先从font来谈起. 如下一段代码: div{ font-size: 14px; font-family: '\5FAE\8F6F\96C5\9ED1'; font-weigh ...
- jquery Mobile点击显示加载等待效果
点击某个按钮或链接时,触发等待加载效果: <script> <!-- $(document).bind("mobileinit", function(){ }); ...
- Ubuntu下很给力的下载工具
Windows下的下载工具--迅雷,之所下面载速度快,乃是它能搜索资源.为己所用,而不是只从原始地址这单一资源处下载. Ubuntu下也有类似的工具,那就是aira2. aira2是一个命令行下载工具 ...
- 导入word数据
public static List<FL> ImportDOC(object fileName,out StringBuilder meg) { List<FL> ...
- LinQ—Lambda表达式
概述 本篇博客主要解说lambda表达式,在这里将它的来龙去脉,主要是从托付,匿名函数这些方面过度讲的,当然,在讲托付和匿名函数的时候,主要是从Lambda的角度出发讲的,可能它们还具有其他的一些作用 ...
- oc-15-枚举结构体
Cat.h #import <Foundation/Foundation.h> // 颜色的枚举 typedef enum{ ColorBlack, ColorYeallow } Colo ...
- XCODE多行代码缩进快捷键
转自:http://xiagudao.com/xcode多行代码缩进快捷键 在XCODE中无法使用TAB键对多行代码进行缩进.想多行缩进,选中需要缩进的代码使用快捷键command(花键)+] 即可右 ...
- SPI的工作模式口诀[转]
口诀:03升,12降,低取小,高取大 ! 解释: 1. 03升,12降: 0.3模式为上升沿采样,1.2模式为下降沿采样. 2. 低取小,高取大:SCK初始电平为低则模式取小值,SCK初始电平为高则模 ...
- Ubuntu下安装FTP服务及使用(以VSFTPD为例)
(一)安装与配置 (1) 安装vsftpd $sudo apt-get install vsftpd 这里会提示无法创建/home/ftp目录,手动创建一个 $sudo mkdir /ho ...
- git-ssh 配置和使用
1.设置Git的user name和email:(如果是第一次的话) $ git config --global user.name "humingx" $ git config ...