C#—委托分析
1.简单委托示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace SimpleTest
{
class Program
{
private delegate string GetAString(); //声明委托
static void Main(string[] args)
{
int x = ;
GetAString delegateString = new GetAString(x.ToString); //委托接受一个参数的构造函数
GetAString delegateString1 = x.ToString; //将方法的地址赋值给委托变量 Tostring()是字符串对象
Console.WriteLine("string is {0}",delegateString());
Console.WriteLine("string1 is {0}", delegateString1());
}
}
}
输出结果

2.委托数组实现多播委托(调用多个方法) 包括func<T>委托方式
操作类(MathOperation)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace SimpleTest1
{
class MathOperation
{
public static double MultiplyByTwo(double value)
{
return value * ;
} public static double Square(double value)
{
return value * value;
}
}
}
测试类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace SimpleTest1
{
class Program
{
delegate double DoubleOP(double value); //声明委托 //将委托传递给方法
//委托作为第一个参数传递
private static void ProcessAndDisplayNumber(DoubleOP action,double value)
{
double result = action(value); //
Console.WriteLine("Value is {0},result of operation is {1}",value,result);
} //利用Func<T>泛型实现委托
private static void ProcessAndDisplayNumber(Func<double, double> action, double value)
{
double result = action(value);
Console.WriteLine("Value is {0},result of operation is {1}", value, result);
} static void Main(string[] args)
{
//方式一
//DoubleOP[] operations = // 实例化一个委托的数组,可以在循环中调用不同的方法
//{
// MathOperation.MultiplyByTwo,
// MathOperation.Square
//}; //方式二
Func<double, double>[] operations = //Func<T>允许调用带返回类型的方法
{
MathOperation.MultiplyByTwo,
MathOperation.Square
}; /*
* i=0的时候,即operations[0]委托的实例为 MathOperation.MultiplyByTwo,
* 调用ProcessAndDisplayNumber(operations[0],2.0)后
* double result = action(value); // action(value)相当于调用MathOperation.MultiplyByTwo(value)
* operations[i] 委托表示的方法,operations[i](2.0) 调用委托的商品
*/
for (int i = ; i < operations.Length; i++)
{
Console.WriteLine("Using operations [{0}]:", i);
ProcessAndDisplayNumber(operations[i], 2.0);
ProcessAndDisplayNumber(operations[i], 7.84);
ProcessAndDisplayNumber(operations[i], 1.414);
Console.WriteLine();
} }
}
}
输出结果

3.Action<T>实现多播委托改上上述测试样例
操作类(MathOperation)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Multicastdelegate
{
class MathOperations
{
public static void MultiplyByTwo(double value)
{
double result = value * ;
Console.WriteLine("[{0}]Multiply by 2 = [{1}]:",value,result);
} public static void Square(double value)
{
double result = value * value;
Console.WriteLine("[{0}]Square = [{1}]",value,result);
}
}
}
测试类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Multicastdelegate
{
class Program
{
/// <summary>
/// 多播委托方式
/// </summary>
/// <param name="action"></param>
/// <param name="value"></param>
public static void ProcessAndDisplayNumber(Action<double> action,double value)
{
Console.WriteLine("ProcessAndDisplayNumber called with value={0}",value);
action(value);
} static void Main(string[] args)
{
Action<double> operations = MathOperations.MultiplyByTwo;
operations+= MathOperations.Square; //+=向委托中添加方法 ProcessAndDisplayNumber(operations,2.0);
ProcessAndDisplayNumber(operations, 4.0);
}
}
}
输出结果:

4.对象排序的委托(冒泡排序)
冒泡算法类(BubbleSorter)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace BubbleSort
{
/// <summary>
/// 冒泡排序
/// </summary>
class BubbleSorter
{
/// <summary>
/// comparison必须引用一个方法,该方法带有两个参数,如果第一个参数“小于”第二个参数返回true
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sortArray"></param>
/// <param name="comparison"></param>
static public void Sort<T>(IList<T> sortArray, Func<T, T, bool> comparison)
{
bool swapped = true;
do
{
swapped = false;
for (int i = ; i < sortArray.Count - ; i++)
{
if (comparison(sortArray[i + ], sortArray[i]))
{
T temp = sortArray[i];
sortArray[i] = sortArray[i + ];
sortArray[i + ] = temp;
swapped = true;
}
}
} while (swapped);
}
}
}
职工信息类(Employee)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace BubbleSort
{
class Employee
{
public string Name { get; set; }
public decimal Salary { get; set; }
public override string ToString()
{
return string.Format("{0},{1:C}", Name, Salary);
} public Employee(string name, decimal salary)
{
this.Name = name;
this.Salary = salary;
} /// <summary>
/// 为了匹配冒泡排序的Func<T, T, bool> comparison必须定义如下方法
/// </summary>
/// <param name="e1"></param>
/// <param name="e2"></param>
/// <returns></returns>
public static bool CompareSalary(Employee e1, Employee e2)
{
return e1.Salary < e2.Salary;
}
} }
测试类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace BubbleSort
{
class Program
{
static void Main(string[] args)
{
Employee[] employees =
{
new Employee("小张", ),
new Employee("小王", ),
new Employee("小李", ),
new Employee("小赵", )
}; BubbleSorter.Sort(employees, Employee.CompareSalary); //利用委托对对象排序 foreach (var employee in employees)
{
Console.WriteLine(employee);
} }
}
}
输出结果:

C#—委托分析的更多相关文章
- jQuery-1.9.1源码分析系列(十) 事件系统——事件委托
jQuery的事件绑定有几个比较优秀的特点: 1. 可以绑定不限数量的处理函数 2. 事件可以委托到祖先节点,不必一定要绑到对应的节点,这样后添加的节点也照样能被处理. 3. 链式操作 下面主要分析事 ...
- 【转】第5篇:Xilium CefGlue 关于 CLR Object 与 JS 交互类库封装报告:自动注册JS脚本+委托回调方法分析
作者: 牛A与牛C之间 时间: 2013-11-19 分类: 技术文章 | 暂无评论 | 编辑文章 主页 » 技术文章 » 第5篇:Xilium CefGlue 关于 CLR Object 与 JS ...
- 【转】第4篇:Xilium CefGlue 关于 CLR Object 与 JS 交互类库封装报告:委托回调方法分析
作者: 牛A与牛C之间 时间: 2013-11-18 分类: 技术文章 | 暂无评论 | 编辑文章 主页 » 技术文章 » 第4篇:Xilium CefGlue 关于 CLR Object 与 JS ...
- C++11实现模板手柄:委托构造函数、defaultkeyword分析
C++11.使用委托构造函数.和高速变量初始化,defaultkeyword重新声明默认构造函数,回答pod状态. 分析与推荐的方法. 到目前为止,VS2012和2013异常声明兼容还是停留在通信代码 ...
- Javascript添加事件的addEventListener()及attachEvent()区别分析,事件委托
Mozilla中: addEventListener的使用方式: target.addEventListener(type, listener, useCapture); target: 文档节点.d ...
- jQuery的事件委托实例分析
事件委托主要是利用事件冒泡现象来实现的,对于事件委托的精准的掌握,可以有利于提高代码的执行效率.先看一段代码实例: <!DOCTYPE html> <html> <hea ...
- js实例分析JavaScript中的事件委托和事件绑定
我们在学习JavaScript中,难免都会去网上查一些资料.也许偶尔就会遇到“事件委托”(也有的称我“事件代理”,这里不评论谁是谁非.以下全部称为“事件委托”),尤其是在查JavaScript的事件处 ...
- [C#]委托实例分析(附源码)
一直都听说C#中的委托与事件非常重要,都没有什么切身的体会,而这次通过做一个WinForm二次开发的项目才真正感觉到了委托与事件的犀利之处. 1.C#中的事件和委托的作用? 事件代表一个组件能够被关注 ...
- 深入浅出面向对象分析与设计读书笔记一&吉他搜索案例&吉他特性锚点集中&委托&重用&业务阶段&需求列表&用例
案例:吉他搜索Guitar Inventory GuitarSpec需求变化:增加吉他弦数特性原始程序需要的变化: 1.修改GuitarSpec,构造,成员,getter 2.修改Guitar,构造, ...
随机推荐
- C# static成员的构造顺序
熟知的几个原则 1.static字段初始化先于static构造函数 2.static字段按顺序初始化 3.static字段和static构造函数只执行一次 public class Foo { pub ...
- 我的VSTO之路:序
原文:我的VSTO之路:序 VSTO是微软提供给.Net开发人员的一个接口,通过他我们可以对Office程序做一些处理.但是这个接口并不尽善尽美,相比微软的很多其他产品,VSTO的稳定性并不好,相关的 ...
- java学习之数组(二)
在上一节中我们讲到了数组的概念,定义,以及在内存当中的表现形式.那么这里我们来说一下,数组的另一种定义方式. 在上一篇当中我们规定是这个样子定义数组的, class ArrDemo { public ...
- Light OJ 1027 - A Dangerous Maze(概率)
题目大意: 你在一个迷宫里,你面前有n个门,你选择门的概率是一样的,每扇门有一个数字k, 加入这个数字是负数,那么这个门会花费你abs(k)分钟后把你带回原点, 假如这个数字是正数,他可以把你带出迷宫 ...
- Java Topology Suite (JTS)与空间数据模型
JTS是Java的处理地理数据的API,它提供以下功能: 实现了OGC关于简单要素SQL查询规范定义的空间数据模型 一个完整的.一致的.基本的二维空间算法的实现,包括二元运算(例如touch和over ...
- 带你一分钟理解 JavaScript 闭包
什么是闭包? 先看一段代码: function a(){ var n = 0; function inc() { n++; console.log(n); } inc(); inc(); } a(); ...
- Monad
Monad英文含义,单体.单分子.单细胞. Monad是一种设计模式,表示将一个运算过程,通过函数拆解成互相连接的多个步骤.你只要提供下一步运算所需的函数,整个运算就会自动进行下去.
- bootstrap 仿实例
bootstrap实现一个网页 html文件 <!DOCTYPE html> <html> <head lang="en"> <meta ...
- nginx 请求负载 转发规则设置
(1)轮询(默认) weight=5; #本机上的Squid开启3128端口,不是必须要squid server 192.168.8.2x:80 weight=1 ...
- SCOI2013 多项式的运算
---恢复内容开始--- 又是一道裸数据结构题. 之前受序列操作的蛋疼写法影响,只用一个tag,不知道怎么记,之后看了下别人的,终于领悟要用两个tag,一个add,一个mul,维护相当简单,想清楚就行 ...