using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApp386
{
class Program
{
static void Main(string[] args)
{
Thermostat thermostat = new Thermostat();
Heater heater = new Heater();
Cooler cooler = new Cooler(); Action<float> del1;
Action<float> del2;
Action<float> del3;
del1 = heater.OnTemperatureChanged;
del2 = cooler.OnTemperatureChanged;
Console.WriteLine("Invoke both delegates:");
del3 = del1+del2; del3();
Console.WriteLine("Multicast delegates:");
foreach(var dl in del3.GetInvocationList())
{
Console.WriteLine($"Target:{dl.Target},Method:{dl.Method}");
} Console.WriteLine();
Console.WriteLine("Invoke only del2:");
del3 = del3 - del1;
del3(); Console.WriteLine("\nSingle delegate:");
foreach (var dl in del3.GetInvocationList())
{
Console.WriteLine($"Target:{dl.Target},Method:{dl.Method}");
} Console.ReadLine();
}
} class Cooler
{
public float Temperature
{
get;set;
}
public Cooler(float temperature)
{
Temperature = temperature;
} public void OnTemperatureChanged(float newTemperature)
{
if(newTemperature>Temperature)
{
Console.WriteLine("Cooloer:On");
}
else
{
Console.WriteLine("Cooler:Off");
}
}
} class Heater
{
public float Temperature
{
get;set;
}
public Heater(float temperature)
{
Temperature = temperature;
} public void OnTemperatureChanged(float newTemperature)
{
if(newTemperature<Temperature)
{
Console.WriteLine("Heater:On");
}
else
{
Console.WriteLine("Heater:Off");
}
}
} public class Thermostat
{
//Define the event publisher
public Action<float> OnTemperatureChange { get; set; } private float currentTemperatureValue;
public float CurrentTemperature
{
get
{
return currentTemperatureValue;
}
set
{
if(value!=currentTemperatureValue)
{
currentTemperatureValue = value;
OnTemperatureChange?.Invoke(value);
}
}
}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApp386
{
class Program
{
static void Main(string[] args)
{
Thermostat thermostat = new Thermostat();
Heater heater = new Heater();
Cooler cooler = new Cooler(); Action<float> del1;
Action<float> del2;
Action<float> del3;
del1 = heater.OnTemperatureChanged;
del2 = cooler.OnTemperatureChanged;
Console.WriteLine("Invoke both delegates:");
del3 = del1;
del3 += del2;
del3();
Console.WriteLine("Multicast delegates:");
foreach(var dl in del3.GetInvocationList())
{
Console.WriteLine($"Target:{dl.Target},Method:{dl.Method}");
}
Console.WriteLine();
Console.WriteLine("Invoke only del2:");
del3 -= del1;
del3(); Console.WriteLine("\nSingle delegate:");
foreach (var dl in del3.GetInvocationList())
{
Console.WriteLine($"Target:{dl.Target},Method:{dl.Method}");
}
Console.ReadLine();
}
} class Cooler
{
public float Temperature
{
get;set;
}
public Cooler(float temperature)
{
Temperature = temperature;
} public void OnTemperatureChanged(float newTemperature)
{
if(newTemperature>Temperature)
{
Console.WriteLine("Cooloer:On");
}
else
{
Console.WriteLine("Cooler:Off");
}
}
} class Heater
{
public float Temperature
{
get;set;
}
public Heater(float temperature)
{
Temperature = temperature;
} public void OnTemperatureChanged(float newTemperature)
{
if(newTemperature<Temperature)
{
Console.WriteLine("Heater:On");
}
else
{
Console.WriteLine("Heater:Off");
}
}
} public class Thermostat
{
//Define the event publisher
public Action<float> OnTemperatureChange { get; set; } private float currentTemperatureValue;
public float CurrentTemperature
{
get
{
return currentTemperatureValue;
}
set
{
if(value!=currentTemperatureValue)
{
currentTemperatureValue = value;
OnTemperatureChange?.Invoke(value);
}
}
}
}
}

C# delegate multicast single delegate的更多相关文章

  1. 在OC中调用Swift类中定义delegate出现:Property 'delegate' not found on object of type ...

    找了许久没找到答案, 在下面的链接中, 我解决了这个问题: http://stackoverflow.com/questions/26366082/cannot-access-property-of- ...

  2. (转)C# Delegate.Invoke、Delegate.BeginInvoke

    Delegate的Invoke.BeginInvoke 1.Delegate.Invoke (委托同步调用) a.委托的Invoke方法,在当前线程中执行委托. b.委托执行时阻塞当前线程,知道委托执 ...

  3. C#Delegate.Invoke、Delegate.BeginInvoke And Control.Invoke、Control.BeginInvoke

    作者:EasonLeung 一.Delegate的Invoke.BeginInvoke 1.Delegate.Invoke (委托同步调用) a.委托的Invoke方法,在当前线程中执行委托. b.委 ...

  4. c# 关键字delegate、event(委托与事件)[MSDN原文摘录][1]

    A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++. ...

  5. JSBinding + SharpKit / 原理篇:Delegate

    以 NGUI 的 UIEventListener 为例: 有一个类: using SharpKit.JavaScript; using UnityEngine; using System.Collec ...

  6. 【转】iOS 开发之协议protocal-代理传值delegate

    原文网址:http://www.cnblogs.com/wzrong/p/3201938.html 刚开始做iOS开发的时候,对 protocol.delegate 的理解一直都是晕晕乎乎一知半解的状 ...

  7. C#中的委托(Delegate)和事件(Event)

    原文地址:C#中的委托(Delegate)和事件(Event) 作者:jiyuan51 把C#中的委托(Delegate)和事件(Event)放到现在讲是有目的的:给下次写的设计模式--观察者(Obs ...

  8. C# Note2:委托(delegate) & Lambda表达式 & 事件(event)

    前言 本文主要讲述委托和Lambda表达式的基础知识,以及如何通过Lambda表达式实现委托调用,并阐述.NET如何将委托用作实现事件的方式. 参考:C#高级编程 1.什么是委托(delegate)? ...

  9. Delegate,Action,Func,匿名方法,匿名委托,事件

    一.委托Delegate 一般的方法(Method)中,我们的参数总是string,int,DateTime...这些基本的数据类型(或者没有参数),比如 public void HelloWorld ...

随机推荐

  1. Springcloud 微服务 高并发(实战1):第1版秒杀

    疯狂创客圈 Java 高并发[ 亿级流量聊天室实战]实战系列之15 [博客园总入口 ] 前言 前言 疯狂创客圈(笔者尼恩创建的高并发研习社群)Springcloud 高并发系列文章,将为大家介绍三个版 ...

  2. dataTable 表插入新行

    DataRow dr = dt.NewRow();//定义新行            dr["sumPrice"] = sumPrice;//对应字段赋值            d ...

  3. OAuth2、OpenID Connect简介

    当我们在登录一些网站的时候,需要第三方的登录.比如,现在我们要登录简书https://www.jianshu.com/sign_in,我们使用微博登录,点击下方的一个微博的小按钮,就会出现这么一个地址 ...

  4. 关于mybtis 使用过程中发生There is no getter for property named 'id' in class 'java.lang.String' 错误

    今天在修改一个关于mybtis语句时,偶然发现的一个错误  There is no getter for property named 'id' in class 'java.lang.String' ...

  5. Java - IO System类支持和缓冲流

    System类的支持和缓冲流 System类对IO的支持 在System类中,为了支持IO操作提供了三个常量: 错误输出: public static final PrintStream err; 输 ...

  6. 持续集成与Devops关系

    什么是持续集成 持续集成(Continuous Integration,简称CI),是一种软件开发实践,在实践中指只要代码有变更,就自动运行构建和测试,反馈运行结果.通俗一点来讲,就是绑定项目的代码仓 ...

  7. 【H5最强攻略】百度人脸情绪实时识别

    最近看的各位大佬都在体验百度大脑2019年全新上线的24项AI能力! (我也按耐不住了,赶紧走一波- 哈哈) 接下来要介绍的就是H5端的人脸检测攻略. 附带详细的介绍,代码,以及演示体验等 欢迎提出各 ...

  8. Spring Boot 2 配置服务器访问日志

    Tomcat控制台中看到的日志是服务器的日志,而服务器访问日志则是记录服务处理的请求信息. 开发环境:IntelliJ IDEA 2019.2.2Spring Boot版本:2.1.8 1.新建一个名 ...

  9. RAC ORA-32004: obsolete or deprecated parameter(s) specified for RDBMS instance

    重启RAC时发现32004,后来才发现DG已经不存在了standby_archive_dest还在生效中,而background_dump_dest和user_dump_dest并没在pfile中出现 ...

  10. Linux:LNMP环境的搭建

    LNMP环境的搭建 安装DNS服务器 安装DNS服务 yum install bind -y DNS的配置 创建正向解析 以创建一个名为"lsy.com"的正向查找区域为例: 第一 ...