c# - Anonymous function delegate profit -
i have button event declared :
mybutton.click += new eventhandler(buttonclicked); private void buttonclicked(object s, eventargs e) { this.close(); }
i can same :
mybutton.click += (s, e) => this.close();
i'm sure advantage of using sec way of doing not aesthetic.
why , when should utilize sec method because i'm confused? if it's of it, doesn't much cleaner when have more 1 instructions in body of anonymous function.
technically there no difference. compiler generate handler method in sec case.
but frankly speaking never utilize anonymous event handlers. why? because not have names. ide can't help me find place event handled. remember exact place subscribed anonymous method? well, perchance remember place. bet teammates don't.
also don't mixing styles of event handlers. visual studio generates me button1_click
methods. don't having handlers subscribed way, , subscribed in place.
and see event argument type. , yes unsubscribing matters sometime. , few more points - anonymous methods useful simple logic (like closing form in case), them become messy more complex. , think putting event handler in place breaks single responsibility of method. subscribes event, other stuff, , handles event in same place. separate things in order create them more readable , maintainable.
c# events lambda anonymous-function
No comments:
Post a Comment