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. UFLDL实验报告3:Self-taught

    Self-taught 自我学习器实验报告 1.Self-taught 自我学习实验描述 自我学习是无监督特征学习算法,自我学习意味着算法能够从未标注数据中学习,从而使机器学习算法能够获得更大数量的数 ...

  2. 使用OpenSSL API进行安全编程

    http://www.ibm.com/developerworks/cn/linux/l-openssl.html OpenSSL API 的文档有些含糊不清.因为还没有多少关于 OpenSSL 使用 ...

  3. linux常用命令(4)rm命令

    rm是一个危险的命令,使用的时候要特别当心,尤其对于新手,否则整个系统就会毁在这个命令(比如在/(根目录)下执行rm * -rf).所以,我们在执行rm之前最好先确认一下在哪个目录,到底要删除什么东西 ...

  4. H3C S5500上层接路由,VLAN IP作网站配置实例

    # version 5.20, Release 2208 # sysname S5500-1 # clock timezone #Web#8#01 add 08:00:00 # super passw ...

  5. H3C S5000和H3C S5500,俺来罗

    S5开头,后面第二位数0-4搂二层交换机.5-9的为三层交换机. 小常识.

  6. activiti入门3排他网关,并行网管,包含网关,事件网关(转)

    网关用来控制流程的流向 网关可以消费也可以生成token. 网关显示成菱形图形,内部有有一个小图标. 图标表示网关的类型. 基本分支 首先 利用 流程变量  写个带有分支的一个基本流程 流程图: 部署 ...

  7. 13. vs2010 ClientID bug处理

    在VS2010中的产生ClientID有几种方式,每个控件或页面有个ClientIDMode属性,可以用来决定产生ClientID的方式,它有AutoID,Static,Inherit,Predict ...

  8. PL/SQL developer export/import (转)

    export/import图标为灰色:原因:相关应用程序没有关联菜单栏 --> Tools --> Import Tables... --> Oracle Import Export ...

  9. zabbix 四张大表分区

    trends_uint.ibd history history_unit trends CREATE TABLE `trends` ( `itemid` bigint(20) unsigned NOT ...

  10. 【HDOJ】2405 Marbles in Three Baskets

    BFS+状态压缩. /* 2405 */ #include <iostream> #include <queue> #include <cstdio> #inclu ...