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. ubuntu 安装谷歌浏览器

    如何在ubuntu 中安装谷歌浏览器, 对于一个刚刚接触ubuntu的人来说,希望安装软件更加的简单,明了.最好能够像在windows下面安装软件一样. 先来介绍一下如何快速的安装谷歌浏览器,以下的方 ...

  2. 分布式文件服务器FastDFS的使用

    分布式项目中涉及到的文件上传与下载,此时使用之前的上传方式,将上传的文件与当前项目所在服务器放在同一个位置,显然不符合分布式项目的理念,此时我们借助FastDFS将上传的文件数据存储到单纯的一个服务器 ...

  3. mysql8安装后如何修改root密码

    mysql5.7.9之后,就没有了password函数,所以,使用传统的password()函数修改root密码的话,就会提示sql错误 UPDATE user SET authentication_ ...

  4. 【NOI 2011】阿狸的打字机

    Problem Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有 \(28\) 个按键,分别印有 \(26\) 个小写英文字母和 B . P 两个字母. ...

  5. 【nodejs原理&源码杂记(8)】Timer模块与基于二叉堆的定时器

    目录 一.概述 二. 数据结构 2.1 链表 2.2 二叉堆 三. 从setTimeout理解Timer模块源码 3.1 timers.js中的定义 3.2 Timeout类定义 3.3 active ...

  6. [WPF 自定义控件]使用WindowChrome自定义RibbonWindow

    1. 为什么要自定义RibbonWindow 自定义Window有可能是设计或功能上的要求,可以是非必要的,而自定义RibbonWindow则不一样: 如果程序使用了自定义样式的Window,为了统一 ...

  7. JVM发生full gc的情景有哪些

    除直接调用System.gc外,触发Full GC执行的情况有如下四种.1. 旧生代空间不足 旧生代空间只有在新生代对象转入及创建为大对象.大数组时才会出现不足的现象,当执行Full GC后空间仍然不 ...

  8. Format a Business Object Caption 设置业务对象标题的格式

    In this lesson, you will learn how to format the caption of a detail form that displays a business o ...

  9. [browser location和history] 简单实现了个路由[转载]

    今天看了1下前面写的,好像缺乏交流性,周末再来弄吧 -0- 今天看了browser 的 location 和 history location属性 // //location.hash 性是一个可读可 ...

  10. 【C#】学习笔记(3) 关于Events使用的小Demo

    关于Events事件的简单Demo. 目录结构: Program.cs using System; namespace EventsDemo { class Program { static void ...