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. 张江在线APP演示

    app下载地址:https://itunes.apple.com/cn/app/zhang-jiang-zai-xian/id722630317?mt=8

  2. 关于MYSQL优化(持续更新)

    *利用MYSQL数据缓存提高效率,注意事项: 1.应用环境:不经常改变的表及对此表相同的查询 2.不适用于服务器端编写的语句 3.根据数据使用频率,合理分解表 4.合理使用默认条件,提高命中率 5.统 ...

  3. 专家解读Linux操作系统内核中的GCC特性

    专家解读Linux操作系统内核中的GCC特性   Linux内核使用GNU Compiler Collection (GCC)套件的几个特殊功能.这些功能包括提供快捷方式和简化以及向编译器提供优化提示 ...

  4. Atlantis

    poj1151:http://poj.org/problem?id=1151 题意:求矩形面积的并题解:扫描线加线段树 #include<iostream> #include<cst ...

  5. Babelfish

    Time Limit: 1000MS Memory limit: 65536K 题目描述You have just moved from Waterloo to a big city. The peo ...

  6. Sublime Text 3 配置

    配置: { "font_face": "Source Code Pro", , "highlight_line": true, " ...

  7. Pots(bfs)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8266   Accepted: 3507   Special Judge D ...

  8. AC+DP练习

    1.HDU 2222 Keywords Search 求目标串中出现了几个模式串. #include<iostream> #include<cstdio> #include&l ...

  9. Android RSA加密对象数据

    前几天说了手头项目用MD5和双向加密DES来进行加密的,因为产品是在不断的改造中完善的.考虑到DES和MD5加解密还是会存下一定的风险,于是产品一拍,不行,听你们说MD5加密是不安全的,咱们产品以后做 ...

  10. nodejs 与 mysql联接

    首先安装Mysql 模块吧 npm install mysql 刚开始在网上搜索了一个测试代码,发现根本就连接不上mysql. varClient=require('mysql').Client, c ...