C#_delegate - 有返回值手工调用
2013-09-27 00:02
423 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace EventClock { public class ClassWithDelegate { //封装了一个返回值为int的多重委托方法 public delegate int DelegateThatReturns(); public DelegateThatReturns theDelegate; public void Run() { for (; ; ) { Thread.Sleep(500); if (theDelegate != null) { //显式调用每个委托方法 foreach (DelegateThatReturns del in theDelegate.GetInvocationList()) { int result = del(); Console.WriteLine("Delegates fired! return result: {0}",result); } Console.WriteLine(); } } } } public class FirstSubscribe { private int myCounter = 0; public void Subscribe(ClassWithDelegate theClassWithDelegate) { theClassWithDelegate.theDelegate += new ClassWithDelegate.DelegateThatReturns(DisplayCounter); } public int DisplayCounter() { return ++myCounter; } } public class SecondSubscribe { private int myCounter = 0; public void Subscribe(ClassWithDelegate theClassWithDelegate) { theClassWithDelegate.theDelegate += new ClassWithDelegate.DelegateThatReturns(Doubler); } public int Doubler() { return myCounter += 2; } } class Program { static void Main(string[] args) { ClassWithDelegate theClassWithDelegate = new ClassWithDelegate(); FirstSubscribe fs = new FirstSubscribe(); fs.Subscribe(theClassWithDelegate); SecondSubscribe ss = new SecondSubscribe(); ss.Subscribe(theClassWithDelegate); theClassWithDelegate.Run(); Console.ReadLine(); } } }
相关文章推荐
- C#调用含有数组类形参和返回值的matlab函数实例
- 【C#基础】方法及其调用、构造方法、out与ref参数及其返回值、方法重载、静态方法等简单介绍
- C#调用存储过程返回值
- C#老生常谈之异步调用的参数及返回值
- C#调用存储过程返回值
- C#调用存储过程返回值
- C#调用C++ 平台调用P/Invoke 结构体--输入输出参数、返回值、返出值、结构体数组作为参数【五】
- C# 动态调用WebService, 不同结果返回值处理
- C#调用存储过程带输出参数或返回值分类(转)
- C#调用存储过程 之返回值与输出参数
- 存储过程输出参数、返回值、返回表及C#调用
- 整理sqlserver 级联更新和删除 c#调用存储过程返回值
- C# 调用C写的DLL时 对应的int* 类型返回值转化为 IntPtr
- C#_delegate - 调用列表
- 关于C++调用C#封装的dll后,获取C#中string返回值的问题
- C# 调用存储过程操作 OUTPUT参数和Return返回值
- 【C#基础】方法及其调用、构造方法、out与ref参数及其返回值、方法重载、静态方法等简单介绍
- C#调用CMD输命令接收返回值
- C#调用C++的DLL函数另一则(delegate) z
- c# 调用SQL Server存储过程返回值(转)