C# delegate multicast single delegate
程序员文章站
2022-03-15 19:27:19
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks... ......
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); } } } } }
上一篇: 新iPhone将于9月19日线下发售!你还会去排队么?
下一篇: 循环队列的双循环链表结构