Is there anything wrong with using lambda for winforms event?

后端 未结 2 1606
抹茶落季
抹茶落季 2021-01-20 05:14

This is a very simple question. I\'m asking because I\'ve never seen it before which makes me wonder if there\'s something wrong.

comboBox1.MouseEnter += (a         


        
2条回答
  •  梦谈多话
    2021-01-20 05:59

    It is fine; the only subtle point is if you need to unsubscribe it; then you need to store the delegate locally too:

    EventHandler handler = (s,a) => ...
    obj.SomeEvent += handler;
    ...
    obj.SomeEvent -= handler;
    

    Note that if I'm not using either parameter (sender/args) I prefer the anon method syntax:

    obj.SomeEvent += delegate {...};
    

    As this doesn't introduce any extra (unnecessary) variables into scope.

提交回复
热议问题