Checking delegates for null

后端 未结 6 1998
滥情空心
滥情空心 2021-02-04 11:48

I was reading the Essential C# 3.0 book and am wondering if this is a good way to check delegates for null?:

class Thermostat
{
    public delegate void Temperat         


        
6条回答
  •  一向
    一向 (楼主)
    2021-02-04 12:50

    I just see a bit of refactoring that could be done but otherwise it looks good...

    class Thermostat
    {
        public delegate void TemperatureChangeHandler ( float newTemperature );
    
        public TemperatureChangeHandler OnTemperatureChange { get; set; }
    
        float currentTemperature;
    
        public float CurrentTemperature
        {
            get { return this.currentTemperature; }
            set
            {
                    if (currentTemperature != value)
                    {
                            currentTemperature = value;
    
                            if (this.OnTemperatureChange != null )
                            {
                                    this.OnTemperatureChange.Invoke( value );
                            }
                    }
            }
        }
    }
    

提交回复
热议问题