C# event

 class Program
    {
        static void Main(string[] args)
        {
            Thermostat thermostat = new Thermostat();
            Heater heater = new Heater(60);
            Cooler cooler = new Cooler(80);

            string temperature;
            thermostat.OnTemperatureChange += heater.OnTemperatureChanged;
            thermostat.OnTemperatureChange += cooler.OnTemperatureChanged;
            Console.WriteLine("Enter temperature: ");
            temperature = Console.ReadLine();
            if(int.TryParse(temperature,out int result))
            {
                thermostat.CurrentTemperature = result;
            }           
            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("Cooler: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(value);
                }
            }
        }
    }

  

 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);
                }
            }
        }
    }
 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;
                    Action<float> localOnChange = OnTemperatureChange;
                    if(localOnChange!=null)
                    {
                        localOnChange(value);
                    }
                }
            }
        }
    }
static void Main(string[] args)
        {
            Thermostat thermostat = new Thermostat();
            Heater heater = new Heater(60);
            Cooler cooler = new Cooler(80);

            Action<float> del1, del2, del3;
            del1 = heater.OnTemperatureChanged;
            del2 = cooler.OnTemperatureChanged;
            Console.WriteLine("Invoke both delegates:");
            del3 = del1;
            del3 += del2;
            del3(90);

            Console.WriteLine("Invoke only del2:");
            del3 -= del2;
            del3(30);
            Console.ReadLine();
        }
static void Main(string[] args)
        {
            Thermostat thermostat = new Thermostat();
            Heater heater = new Heater(60);
            Cooler cooler = new Cooler(80);

            Action<float> del1, del2, del3;
            del1 = heater.OnTemperatureChanged;
            del2 = cooler.OnTemperatureChanged;

            Console.WriteLine("Combine delegates using + operator");
            del3 = del1 + del2;
            del3(60);

            Console.WriteLine("UnCombine delegates using - operator!");
            del3 = del3 - del2;
            del3(30);
            Console.ReadLine();
        }
 static void Main(string[] args)
        {
            Thermostat thermostat = new Thermostat();
            Heater heater = new Heater(60);
            Cooler cooler = new Cooler(80);

            thermostat.OnTemperatureChange += heater.OnTemperatureChanged;

            thermostat.OnTemperatureChange += (newTemperature) =>
            {
                throw new InvalidOperationException();
            };

            thermostat.OnTemperatureChange += cooler.OnTemperatureChanged;
            Console.WriteLine("Enter temperature: ");
            string temperature = Console.ReadLine();
            if(int.TryParse(temperature,out int temp))
            {
                thermostat.CurrentTemperature = temp;
            }

            Console.ReadLine();
        }
private float currentTemperatureValue;
        public float CurrentTemperature
        {
            get
            {
                return currentTemperatureValue;
            }
            set
            {
                if(value!=currentTemperatureValue)
                {
                    currentTemperatureValue = value;
                    Action<float> localOnChange = OnTemperatureChange;
                    if(localOnChange!=null)
                    {
                        List<Exception> exceptionCollection = new List<Exception>();
                        foreach(Action<float> handler in OnTemperatureChange.GetInvocationList())
                        {
                            try
                            {
                                handler(value);
                            }
                            catch(Exception ex)
                            {
                                exceptionCollection.Add(ex);
                            }
                        }

                        if(exceptionCollection.Count>0)
                        {
                            throw new AggregateException("There were exceptions throw OnTemperatureChange Event subscribers.", exceptionCollection);
                        }
                    }
                }
            }
        }

 

posted @ 2019-11-20 20:02  FredGrit  阅读(161)  评论(0编辑  收藏  举报