委托及多路广播委托
来源:互联网 发布:it工作经历 编辑:程序博客网 时间:2024/05/04 11:49
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace delegate1{ public delegate int Transformer(int x); public class Util { public static void Transform(int[] value, Transformer t) { for (int i = 0; i < value.Length; i++) { value[i] = t(value[i]); } } } class Program { static int Square(int x) { System.Console.WriteLine("once" + x); return x * x; } static void Main(string[] args) { int[] value = new int[3] { 1, 2, 3 }; Transformer t = Square; t += Square; Util.Transform(value, t); foreach (int item in value) System.Console.WriteLine(item); } }}
输出:
once1
once1
once2
once2
once3
once3
1
4
9
说明 都调用完毕之后才返回的,参数不回发生变化。如若改为引用类型:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace delegate1{ public delegate void Transformer(ref int x); public class Util { public static void Transform(int[] value, Transformer t) { for (int i = 0; i < value.Length; i++) { t(ref value[i]); } } } class Program { static void Square(ref int x) { System.Console.WriteLine("once" + x); x = x * x; } static void Main(string[] args) { int[] value = new int[3] { 1, 2, 3 }; Transformer t = Square; t += Square; Util.Transform(value, t); foreach (int item in value) System.Console.WriteLine(item); } }}
返回:
once1
once1
once2
once4
once3
once9
1
16
81
- 委托及多路广播委托
- C# 委托,合并委托(多路广播委托)
- 委托之 合并委托(多路广播委托)
- 委托及多播委托
- 关于委托的补充及多路委托的实现
- 委托和多路委托
- 如何:合并委托(多路广播委托)(C# 编程指南)
- 黑马笔记-发问-.net委托支持多路广播? 多路广播是什么啊
- 委托(委托变量、多路委托变量)
- C#委托及事件委托
- 委托
- 委托
- 委托
- 委托
- 委托
- 委托
- 委托
- 委托
- iOS 定位服务
- 为YUM设置代理的方法 ccproxy
- 颜色渐变效果
- IOS 数据持久化
- (转)九种引人瞩目的开源大数据技术
- 委托及多路广播委托
- 我和你(4)
- ubuntu network
- oracle游标的使用
- Windows下动态链接
- 用mac前要做的事
- VBscript常用函数
- 程序员的年度计划
- Ubuntu “Wired Network – device not managed” 问题