码迷,mamicode.com
首页 > Windows程序 > 详细

C# delegate multicast single delegate

时间:2019-11-19 11:45:04      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:var   private   off   linq   using   multicast   eric   collect   oid   


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# delegate multicast single delegate

标签:var   private   off   linq   using   multicast   eric   collect   oid   

原文地址:https://www.cnblogs.com/Fred1987/p/11888252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!