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 [导读] 许多人都说 算法是程序的核心,一个程序 ...
随机推荐
- win7远程桌面连接
远程桌面连接 1.在防火墙上设置同意同意远程桌面通过防火墙: 2.为管理员设置password.以便訪问者訪问: 3.我的电脑-右键属性-远程设置-远程-选上"同意远程协助远程这台计算机.同 ...
- javascript之处理Ajax错误
使用Ajax须留心两类错误.它们的差别源于视角的不同. 第一类错误是从XMLHttpRequest对象的角度看到的问题:某些因素阻止了请求发送到server,比如DNS无法解析主机名,连接请求被拒绝. ...
- FindMe
https://github.com/hongdong/FindMe_Android https://github.com/hongdong/FindMe_Server https://github. ...
- Redis 安装与简单示例 <第一篇>
一.Redis的安装 Redis下载地址如下:https://github.com/dmajkic/redis/downloads 解压后根据自己机器的实际情况选择32位或者64位.下载解压后图片如下 ...
- HTML之一语言代码
HTML的lang属性可用于网页或部分网页的语言.这对搜索引擎和浏览器是有帮助的. 同时也可以是指HTTP Header中的Accept-Language/Content-Language. ISO ...
- jquery easyui from 表单返回乱码!
如果用easyui的form进行提交,必须在<form>标签中加入属性method="post",即<form method="post"&g ...
- boa安装
Boa 下载地址:http://www.boa.org/boa-0.94.13.tar.gz 1.解压生成Makefile tar xzf boa-0.94.13.tar.gz #解压 cd boa- ...
- Adding DTrace Probes to PHP Extensions
By cj on Dec 06, 2012 The powerful DTrace tracing facility has some PHP-specific probes that can b ...
- 字典树(Trie)的java实现
一.定义 字典树又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:利用 ...
- Winform下实现图片切换特效的方法
本文实例讲述了Winform下实现图片切换特效的方法,是应用程序开发中非常实用的一个功能.分享给大家供大家参考之用.具体方法如下: 本实例源自网络,功能较为齐全.丰富!主要功能代码如下: using ...