C# why shall I use “new” keyword when subscribing for an event?

前端 未结 4 1563
长情又很酷
长情又很酷 2021-01-14 01:23

What is the difference between following 2 ways of subscribing for an event?

receiver.ConfigChanged += Config_ConfigChanged;

receiver.ConfigChanged += new E         


        
4条回答
  •  伪装坚强ぢ
    2021-01-14 02:25

    Correct me if i'm wrong, but i don't know if this works

    receiver.ConfigChanged += new EventHandler(Config_ConfigChanged);
    receiver.ConfigChanged -= new EventHandler(Config_ConfigChanged);
    

    Since these are 2 different instances I think this would

    var configChanged = new EventHandler(Config_ConfigChanged);
    receiver.ConfigChanged += configChanged;
    receiver.ConfigChanged -= configChanged;
    

    But then again, why not just use

    receiver.ConfigChanged += Config_ConfigChanged;
    receiver.ConfigChanged -= Config_ConfigChanged;
    

提交回复
热议问题