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 [导读] 许多人都说 算法是程序的核心,一个程序 ...
随机推荐
- Trie树学习2
数组实现的Trie树 字符容量有限,能够使用链表实现更为大容量的Trie #include <iostream> #include <cstdio> #include < ...
- 【转】Python实现的线程池
import Queue, threading, sys from threading import Thread import time,urllib # working thread class ...
- 数据结构复习:直接插入排序与二分插入排序的C++实现
1.直接插入排序 直接插入排序的过程可以理解为一个固定长度的数组被分为两个集合,即已排序集合和未排序. 开始时已排序集合为空,而未排序集合即为整个数组.当排序开始后插入一个对象,已排序集合元素数目加1 ...
- 「译」JavaScript 的怪癖 1:隐式类型转换
原文:JavaScript quirk 1: implicit conversion of values 译文:「译」JavaScript 的怪癖 1:隐式类型转换 译者:justjavac 零:提要 ...
- Web开发者不可不知的15条编码原则
HTML已经走过了近20的发展历程.从HTML4到XHTML,再到最近十分火热的HTML5,它几乎见证了整个互联网的发展.但是,即便到现在,有很多基础的概念和原则依然需要开发者高度注意.下面,向大家介 ...
- BZOJ 1053: [HAOI2007]反素数ant dfs
1053: [HAOI2007]反素数ant 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1053 Description 对于任何正整 ...
- Java读取Properties配置文件
1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,使用键值对的形式来保存属性集.不过Properties的键和值都是字符串 ...
- 免费LInux主机资源
一.m-net.arbornet.org注冊 (1)telnet m-net.arbornet.org vista系统默认是关闭telnet的(由于不安全),须要开启.cmd->telnet(2 ...
- TOJ3650 Legal or Not
Legal or Not Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByte Total Submit: 41 ...
- JAVA设置SimpleDateFormat函数时区
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdf.setTimeZone(TimeZo ...