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#_delegate - 有返回值手工调用的更多相关文章

  1. Python3基础 定义无参数无返回值函数 调用会输出hello world的函数

    镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...

  2. Shell函数:Shell函数返回值、删除函数、在终端调用函数

    函数可以让我们将一个复杂功能划分成若干模块,让程序结构更加清晰,代码重复利用率更高.像其他编程语言一样,Shell 也支持函数.Shell 函数必须先定义后使用. Shell 函数的定义格式如下: f ...

  3. Shell函数返回值、删除函数、在终端调用函数

    Shell 也支持函数.Shell 函数必须先定义后使用. Shell 函数的定义格式如下: function_name () { list of commands [ return value ] ...

  4. 【Shell脚本学习22】Shell 函数:Shell函数返回值、删除函数、在终端调用函数

    函数可以让我们将一个复杂功能划分成若干模块,让程序结构更加清晰,代码重复利用率更高.像其他编程语言一样,Shell 也支持函数.Shell 函数必须先定义后使用. Shell 函数的定义格式如下: f ...

  5. Autorelease返回值的快速释放机制

    + (instancetype)createSark { return [self new];}// callerSark *sark = [Sark createSark]; 编译器改写成了形如下面 ...

  6. Shell入门教程:Shell函数的返回值

    shell函数返回值一般有3种方式: 1.return语句(默认的返回值) shell函数的返回值可以和其他语言的返回值一样,通过return语句返回. 比如: #!/bin/bash functio ...

  7. 获取layer.open弹出层的返回值

    正在开发的车联网项目用到了layer API.当我在开发“新建电子围栏”的时候需要弹出地图,用户在地图中画一个区域,最后将这个弹出层的数据返回给原页面.下面是我的实现过:程: 触发弹出层的代码: la ...

  8. ThinkPHP中数据库操作返回值总结

    转自:http://www.baiwar.com/post/thinkphp-database-operations-in-the-return-value.html Thinkphp中的Think\ ...

  9. 【转】ThinkPHP中数据库操作返回值总结

    Thinkphp中的Think\Model类提供了数据库的基本CURD(Create.Update.Read及Delete),通过该类可以很便捷的进行操作.Model类及扩展类主要的方法有: Crea ...

随机推荐

  1. RMAN catalog备份恢复方案

    对于数据库的稳定性,高可用,跨平台以及海量数据库的处理,Oracle 数据库通常是大型数据库和大企业的首选.尽管如此,仍然不乏很多中小企业想要品尝一下Oracle腥味,因此在Oracle环境中也有不少 ...

  2. asp.net微信公众平台开发

    http://mp.weixin.qq.com/wiki/index.php?title=%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E6%8C%87%E5%8D%97 ...

  3. 9Patch在Android平台的应用

  4. tcxtreelist 展示图片 图像

    procedure TfrmstPostImageCollate.LoadCollateImage(AOrderID: integer);var mySql: string; node: TcxTre ...

  5. lightoj 1020 (博弈)

    思路:很简单的博弈,找出每个人先拿的必胜态进行状态转移即可. #include<cstdio> #include<string> #include<cstring> ...

  6. 《Python基础教程(第二版)》学习笔记 -> 第三章 使用字符串

    本章讲话介绍如何使用字符串格式化其他的值,并简单了解一下利用字符串的分割.联接.搜索等方法能做些什么. 基本字符串操作 所有标准的序列操作(索引.分片.乘法.判断成员资格.求长度.取最大最小值)对字符 ...

  7. js中的继承2--原型继承

    一. 原型与构造函数 Js所有的函数都有一个prototype属性,这个属性引用了一个对象,即原型对象,也简称原型.这个函数包括构造函数和普通函数,我们讲的更多是构造函数的原型,但是也不能否定普通函数 ...

  8. activemq 一个不错的ppt

    http://people.apache.org/~jstrachan/talks/ActiveMQ-Dublin07.pdf

  9. AIDL:Binder invocation to an incorrect interface

    Android进程之间通信异常:主要原因是客户端的aidl文件和与远程调用的Service的aidl文件包名不同 处理方式一般就是在客户端要一个与远程暴露出来的接口包名要一致  服务端: 客户端:

  10. java@ What are C++ features missing in Java

    Following features of C++ are not there in Java. No pointers No sizeof operator No scope resolution ...