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. (3)java棧

    java棧和函数调用的关系图 [名词解释]--->java棧是一块线程的私有空间--->java的棧是先进后出的数据结构.函数返回,则该函数的棧帧被弹出.--->一个函数对应一个棧帧 ...

  2. shell脚本编写笔记

    包含转载内容,转载自http://wenku.baidu.com/link?url=jtCHxEYzgGve6P64U3JRQRgU6nhpGvqFLLpWu9I2Htq6hi9TGLudRFkk7r ...

  3. Spring--通过注解来配置bean

    Spring通过注解配置bean 基于注解配置bean 基于注解来配置bean的属性 在classpath中扫描组件 组件扫描(component scanning):Spring能够从classpa ...

  4. MS-SQL索引类型

    一.索引的概念     索引就是加快检索表中数据的方法.数据库的索引类似于书籍的索引.在书籍中,索引允许用户不必翻阅完整个书就能迅速地找到所需要的信息.在数据库中,索引也允许数据库程序迅速地找到表中的 ...

  5. 【Linux.Python】Python进程后台启动

    嗯,比较忧伤. 前几天写了个tornado,启动了,很开心,后来每天要用时都发现it是kill掉的.好吧,是我太蠢啦.百度了下资料 python的启动方式: 1 python yourfile.py ...

  6. 2014年25 款最新最棒的jQuery插件

    网络上提供了大量非常有用的 jQuery 插件,帮助大家完善网站的体验.所以我们在这里收集了 2014 年发布的,并且是非常有用的插件,希望能帮助大家找到自己需要并且喜欢的,提升网站的质量! HAMM ...

  7. Android编译过程详解(一)

    Android编译过程详解(一) 注:本文转载自Android编译过程详解(一):http://www.cnblogs.com/mr-raptor/archive/2012/06/07/2540359 ...

  8. Windows下Android SDK Manage下载速度缓慢的解决方法

    在SDK Manager下Tools->Options打开了SDK Manager的Settings,选中“Force https://… sources to be fetched using ...

  9. C 头文件阅读理解

    __BEGIN_DECLS ..... ..... __END_DECLS 很多时候,为了使 C 代码和 C++ 代码保持互相兼容的过程调用接口,需要在 C++ 代码里加上 extern " ...

  10. adb不响应

    1.CMD命令窗口输入:adb nodaemon server .然后就会提示你哪个端口被占用了. 2.输入netstat -ano | findstr "5037" .然后会弹出 ...