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#—委托分析的更多相关文章

  1. jQuery-1.9.1源码分析系列(十) 事件系统——事件委托

    jQuery的事件绑定有几个比较优秀的特点: 1. 可以绑定不限数量的处理函数 2. 事件可以委托到祖先节点,不必一定要绑到对应的节点,这样后添加的节点也照样能被处理. 3. 链式操作 下面主要分析事 ...

  2. 【转】第5篇:Xilium CefGlue 关于 CLR Object 与 JS 交互类库封装报告:自动注册JS脚本+委托回调方法分析

    作者: 牛A与牛C之间 时间: 2013-11-19 分类: 技术文章 | 暂无评论 | 编辑文章 主页 » 技术文章 » 第5篇:Xilium CefGlue 关于 CLR Object 与 JS ...

  3. 【转】第4篇:Xilium CefGlue 关于 CLR Object 与 JS 交互类库封装报告:委托回调方法分析

    作者: 牛A与牛C之间 时间: 2013-11-18 分类: 技术文章 | 暂无评论 | 编辑文章 主页 » 技术文章 » 第4篇:Xilium CefGlue 关于 CLR Object 与 JS ...

  4. C++11实现模板手柄:委托构造函数、defaultkeyword分析

    C++11.使用委托构造函数.和高速变量初始化,defaultkeyword重新声明默认构造函数,回答pod状态. 分析与推荐的方法. 到目前为止,VS2012和2013异常声明兼容还是停留在通信代码 ...

  5. Javascript添加事件的addEventListener()及attachEvent()区别分析,事件委托

    Mozilla中: addEventListener的使用方式: target.addEventListener(type, listener, useCapture); target: 文档节点.d ...

  6. jQuery的事件委托实例分析

    事件委托主要是利用事件冒泡现象来实现的,对于事件委托的精准的掌握,可以有利于提高代码的执行效率.先看一段代码实例: <!DOCTYPE html> <html> <hea ...

  7. js实例分析JavaScript中的事件委托和事件绑定

    我们在学习JavaScript中,难免都会去网上查一些资料.也许偶尔就会遇到“事件委托”(也有的称我“事件代理”,这里不评论谁是谁非.以下全部称为“事件委托”),尤其是在查JavaScript的事件处 ...

  8. [C#]委托实例分析(附源码)

    一直都听说C#中的委托与事件非常重要,都没有什么切身的体会,而这次通过做一个WinForm二次开发的项目才真正感觉到了委托与事件的犀利之处. 1.C#中的事件和委托的作用? 事件代表一个组件能够被关注 ...

  9. 深入浅出面向对象分析与设计读书笔记一&吉他搜索案例&吉他特性锚点集中&委托&重用&业务阶段&需求列表&用例

    案例:吉他搜索Guitar Inventory GuitarSpec需求变化:增加吉他弦数特性原始程序需要的变化: 1.修改GuitarSpec,构造,成员,getter 2.修改Guitar,构造, ...

随机推荐

  1. event.preventDefault和恢复元素默认事件

    写页面事件的时候,有的时候需要用event.preventDefault取消原有的事件后进行重写,这个大家应该都知道. 那么怎么在取消默认事件后再恢复呢. 解绑我们自定义的事件就好了. 以Jquery ...

  2. C语言连接数据库

    举个例子,连接SQL: // 打开数据库 strDBClass.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Jet ...

  3. pycharm去掉拼写检查

    http://zhidao.baidu.com/question/523436629.html

  4. hadoop 异常 datanode未启动

    暴力方法:(本人是学习阶段,实际工作中不能这么做)在各个节点上执行如下操作. 将/tmp 删除 将 conf/mapred-site.xml <property> <name> ...

  5. Windows的免費hMailServer搭配SpamAssassin過濾垃圾郵件:安裝與設定

    http://jdev.tw/blog/1677/hmailserver-with-spamassassin-sawin32 為了解決垃圾信泛濫的狀況,這兩天「跨界」測試了運行在Windows上的免費 ...

  6. BZOJ 1588 营业额统计

    Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每 ...

  7. [BZOJ 1055] [HAOI2008] 玩具取名 【记忆化搜索】

    题目链接:BZOJ - 1055 题目分析 这种类似区间 DP 的记忆化搜索都是很相近的,比如字符串压缩和字符串扩展都差不多. 都是将现在 Solve 的区间分成子区间,再求解子区间. 这道题 Sol ...

  8. Kernel PCA 原理和演示

    Kernel PCA 原理和演示 主成份(Principal Component Analysis)分析是降维(Dimension Reduction)的重要手段.每一个主成分都是数据在某一个方向上的 ...

  9. Eclipse调试的一些小技巧

    不要使用System.out.println作为调试工具 启用所有组件的详细的日志记录级别 使用一个日志分析器来阅读日志 1.条件断点 想象一下我们平时如何添加断点,通常的做法是双击行号的左边.在de ...

  10. c# 绘图常用对象和方法

    //BitMap 位图,常用的方法,     Save:主要方式有:(1)保存在图像文件里,可以指定格式[gif,bmp]:(2) 保存在流中,以指定格式[gif,bmp]         //gra ...