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 [导读] 许多人都说 算法是程序的核心,一个程序 ...
随机推荐
- Android Volley框架的使用(三)
此博文源码下载地址 https://github.com/Javen205/VolleyDemo.git Image Request 为了更方便的使用Volley中的图片请求,我们同样先在Voll ...
- 关于javaScript注册事件传递参数的浅析
最近这半年作为一个java 程序员,我写的javaScript代码都快比java代码多了,前段时间是给某银行做一个柜员管控系统,在柜员授权这一块功能上,由于柜员的授权需要考虑各方面的因素,比如机构权限 ...
- Codeforces Educational Codeforces Round 5 A. Comparing Two Long Integers 高精度比大小,模拟
A. Comparing Two Long Integers 题目连接: http://www.codeforces.com/contest/616/problem/A Description You ...
- 安卓 SQLite数据库操作实例
前段时间写了个安卓平台下SQLite数据库操作的实例 ,一直没得时间总结 ,今天把它弄出来了. 在Android 运行时环境包含了完整的 SQLite. 首先介绍一下SQLite这个数据库: SQLi ...
- Google Map和桌面组件 Android开发教程
本文节选于机械工业出版社推出的<Android应用开发揭秘>一 书,作者为杨丰盛.本书内容全面,详细讲解了Android框架.Android组件.用户界面开发.游戏开发.数据存储.多媒体开 ...
- Oracle DB 管理数据库的空间
• 描述4 KB 扇区磁盘的概念及使用 • 使用可移动表空间 • 描述可移动表空间的概念 数据库存储 数据库存储 数据库包括物理结构和逻辑结构.由于物理结构和逻辑结构是分开的,因此管理数据的物 理存储 ...
- 解决CSDN的code功能,无法git clone多个项目的问题
几天前在使用CSDN的git功能的时候发现一个问题:我在CSDN上创建了两个项目,但是却只能git clone其中的一个. 原因: 在添加ssh公钥的时候,将主机上的ssh公钥在CSDN上填的地方不合 ...
- C++面向对象设计
一. 组合(复合),继承,委托 1.composition(组合)has-a 1.1 组合举例:(Adapter 设计模式) 关系: 利用deque功能实现所有queue功能 template < ...
- 【如何快速的开发一个完整的 iOS 直播 app】(美颜篇)
来源:袁峥Seemygo 链接:http://www.jianshu.com/p/4646894245ba 前言 在看这篇之前,如果您还不了解直播原理,请查看这篇文章如何快速的开发一个完整的iOS直播 ...
- 顺丰快递单号查询api对接(全代码)
接口支持的消息接收方式:HTTP POST 请求方法的编码格式(utf-8):"application/x-www-form-urlencoded;charset=utf-8" 请 ...