Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should event handlers be decorated with their delegate?

Both of the following variations compile and on the surface seem to behave in the same way. Aside from syntax sugar are there any other differences?

someObject.SomeEvent += new SomeEventHandler(someObject_SomeEvent);
someObject.SomeEvent += someObject_SomeEvent;
like image 257
Lea Hayes Avatar asked Apr 29 '13 15:04

Lea Hayes


2 Answers

The two are exactly the same. If you use the second (shorter) form, the compiler just puts in the delegate type for you.

There is no reason to use the syntax which explicitly constructs the delegate, unless you wish the delegate type to exist within the code (for readability, etc).

like image 120
Reed Copsey Avatar answered Sep 22 '22 13:09

Reed Copsey


The new, shorter version of creating a delegate has been introduced with the vs2003 compiler as far as I remember. It is just a syntactic sugar over the longer version.

like image 26
Wiktor Zychla Avatar answered Sep 19 '22 13:09

Wiktor Zychla