C# delegate multicast single delegate

来源:https://www.cnblogs.com/Fred1987/archive/2019/11/19/11888252.html
-Advertisement-
Play Games

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);
                }
            }
        }
    }
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Newtonsoft是我們開發過程中經常用到的一個第三方類庫,主要用於對象的序列化和反序列化。 命名方式 預設情況下序列化後的json字元串會以類名、屬性名作為鍵來命名。問題在於C#的命名規範中類名、屬性名都是以PascalCase方式來命名的,而在前端中一般都是以CamelCase方式來命名的,所 ...
  • 場景 指定一個路徑和尾碼名,查找這個路徑下所有以此尾碼名結尾的文件。 註: 博客主頁: https://blog.csdn.net/badao_liumang_qizhi 關註公眾號 霸道的程式猿 獲取編程相關電子書、教程推送與免費下載。 實現 新建工具類FileHelper,工具類中新建方法Get ...
  • PDF是當今最流行的文檔格式之一,各種應用程式將其用作最終輸出。由於支持多種數據類型和可移植性,因此它是創建和共用內容的首選格式。作為對開發文檔管理應用程式感興趣的.NET應用程式開發人員,可能希望嵌入處理功能,以讀取PDF文檔並將其轉換為其他文件格式,例如HTML。 Aspose.PDF for ...
  • 參考 Abp vNext框架 應用程式開發教程 創建項目和書籍列表頁面 http://www.vnfan.com/helinbin/d/3579c6e90e1d23ab.html 官方源碼 https://github.com/abpframework/abp/tree/dev 開發環境 vs201 ...
  • 很高興與大家分享Java平臺的Aspose.Words最新版v19.11實現了另一個裡程碑,該版本在基於Unix的操作系統上支持Harfbuzz Shaper插件,並使開發人員能夠使用Web擴展,自定義水平格式規則形狀等。接下來,我們一起來聊聊新版本的新功能。 Aspose.Words for Ja ...
  • 場景 Winform中設置ZedGraph滑鼠雙擊獲取距離最近曲線上的點的坐標值: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102466406 現在要實現滑鼠懸浮時顯示距離最近曲線上的點的橫縱坐標和X軸和Y軸的標題。 註: ...
  • 參考 Abp vNext框架 從空項目開始 使用ASP.NET Core Web Application http://www.vnfan.com/helinbin/d/745b1e040c9b4f62.html rynowak的回答 https://github.com/aspnet/AspNet ...
  • Swagger其實包含了三個部分,分別是Swagger Editor文檔介面編輯器,根據介面文檔生成code的Swagger Codegen,以及生成線上文檔的Swagger UI。在AspNetCore中通常使用Microsoft封裝的Swashbuckle來使用Swagger UI,這是一個As ...
一周排行
    -Advertisement-
    Play Games
  • .Net8.0 Blazor Hybird 桌面端 (WPF/Winform) 實測可以完整運行在 win7sp1/win10/win11. 如果用其他工具打包,還可以運行在mac/linux下, 傳送門BlazorHybrid 發佈為無依賴包方式 安裝 WebView2Runtime 1.57 M ...
  • 目錄前言PostgreSql安裝測試額外Nuget安裝Person.cs模擬運行Navicate連postgresql解決方案Garnet為什麼要選擇Garnet而不是RedisRedis不再開源Windows版的Redis是由微軟維護的Windows Redis版本老舊,後續可能不再更新Garne ...
  • C#TMS系統代碼-聯表報表學習 領導被裁了之後很快就有人上任了,幾乎是無縫銜接,很難讓我不想到這早就決定好了。我的職責沒有任何變化。感受下來這個系統封裝程度很高,我只要會調用方法就行。這個系統交付之後不會有太多問題,更多應該是做小需求,有大的開發任務應該也是第二期的事,嗯?怎麼感覺我變成運維了?而 ...
  • 我在隨筆《EAV模型(實體-屬性-值)的設計和低代碼的處理方案(1)》中介紹了一些基本的EAV模型設計知識和基於Winform場景下低代碼(或者說無代碼)的一些實現思路,在本篇隨筆中,我們來分析一下這種針對通用業務,且只需定義就能構建業務模塊存儲和界面的解決方案,其中的數據查詢處理的操作。 ...
  • 對某個遠程伺服器啟用和設置NTP服務(Windows系統) 打開註冊表 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer 將 Enabled 的值設置為 1,這將啟用NTP伺服器功 ...
  • title: Django信號與擴展:深入理解與實踐 date: 2024/5/15 22:40:52 updated: 2024/5/15 22:40:52 categories: 後端開發 tags: Django 信號 松耦合 觀察者 擴展 安全 性能 第一部分:Django信號基礎 Djan ...
  • 使用xadmin2遇到的問題&解決 環境配置: 使用的模塊版本: 關聯的包 Django 3.2.15 mysqlclient 2.2.4 xadmin 2.0.1 django-crispy-forms >= 1.6.0 django-import-export >= 0.5.1 django-r ...
  • 今天我打算整點兒不一樣的內容,通過之前學習的TransformerMap和LazyMap鏈,想搞點不一樣的,所以我關註了另外一條鏈DefaultedMap鏈,主要調用鏈為: 調用鏈詳細描述: ObjectInputStream.readObject() DefaultedMap.readObject ...
  • 後端應用級開發者該如何擁抱 AI GC?就是在這樣的一個大的浪潮下,我們的傳統的應用級開發者。我們該如何選擇職業或者是如何去快速轉型,跟上這樣的一個行業的一個浪潮? 0 AI金字塔模型 越往上它的整個難度就是職業機會也好,或者說是整個的這個運作也好,它的難度會越大,然後越往下機會就會越多,所以這是一 ...
  • @Autowired是Spring框架提供的註解,@Resource是Java EE 5規範提供的註解。 @Autowired預設按照類型自動裝配,而@Resource預設按照名稱自動裝配。 @Autowired支持@Qualifier註解來指定裝配哪一個具有相同類型的bean,而@Resourc... ...