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,构造, ...
随机推荐
- 项目知识点.Part2
1. 取消collectionView头视图重叠情况:以下两种情况效果一样 但是有一点点bug 每次remove之后 需要把视图刷到上面才会显示(后续会改进方法) for (UIView *view ...
- A Statistical View of Deep Learning (II): Auto-encoders and Free Energy
A Statistical View of Deep Learning (II): Auto-encoders and Free Energy With the success of discrimi ...
- 7.DropDownList的绑定
ListView中是无法像TextBox等控件那样将DropDownList的选中值绑定到数据字段的,必须编程处理.如例子:人员的性别(男,女,保密),三个值固定写在DropDownList中. 在显 ...
- 用c写99乘法表
int main(int argc,char **argv){ int a; for(a=1;a<=9;a++){ int b; for(b=1;b<=a;b++){ printf(&qu ...
- ZOJ-2587-Unique Attack(最小割的唯一性)
题意: 求无向图最小割是否唯一 分析: 1.我们先对原图求一次最大流 2.对残留网络,我们从S开始,找到所有所有S能到达的点:再从T开始,找出所有能到达T的点. 3.判断原网络中是否还有没有访问到的点 ...
- 数据结构(动态树):[国家集训队2012]tree(伍一鸣)
[问题描述] 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原 ...
- IIS 启用或关闭目录浏览
如果不希望启用目录浏览,请确保配置了默认文档并且该文件存在. 使用 IIS 管理器启用目录浏览. 打开 IIS 管理器. 在“功能”视图中,双击“目录浏览”. 在“目录浏览”页上,在“操作”窗格中单击 ...
- pgAdminIII使用图解
原文地址:http://www.2cto.com/database/201312/267218.html pgAdmin III简介 要打开一个到服务的连接,在树中选择所需的服务,并双击它,或使用“工 ...
- about variables
局部变量(Local Variable),全局变量(global variable),变量共享; 静态局部变量(static local variables),函数运行结束变量值不会消失,并且其它函数 ...
- windows下python安装paramiko
Python中使用SSH需要用到OpenSSH,而OpenSSH依赖于paramiko模块,而paramiko模块又依赖于pycrypto模块,因此要在Python中使用SSH,则需要先安装模块顺序是 ...