-
C#教程之C# delegate multicast single delegate
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
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(60); Cooler cooler = new Cooler(80); Action<float> del1; Action<float> del2; Action<float> del3; del1 = heater.OnTemperatureChanged; del2 = cooler.OnTemperatureChanged; Console.WriteLine("Invoke both delegates:"); del3 = del1+del2; del3(90); 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(30); 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(60); Cooler cooler = new Cooler(80); 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(90); 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(30); 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#扩展方法学习笔记
C# delegate multicast single delegate
Winform中设置ZedGraph鼠标悬浮显示举例最近
C#中获取指定路径下指定后缀名的所有文
使用Newtonsoft序列化
C#函数(构造函数)的重载
Geometry 判断几何是否被另一个几何/线段分
C# 矢量图EMF 总结
C#泛型自己的理解和总结
数据结构之Array、ArrayList、List、LinkedLis
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
如何远程连接SQL Server数据库的图文教程
复制SqlServer数据库的方法
搜索sql语句
sql中返回参数的值
sql中生成查询的模糊匹配字符串
数据定义功能
数据操作功能
将Session值储存于SQL Server中
使用SQL Server数据库嵌套子查询的方法
SQL概述