Should event handlers be decorated with their delegate?

Both of the following compilation options and on the surface seem to behave the same. Besides sugar syntax, are there any other differences?

someObject.SomeEvent += new SomeEventHandler(someObject_SomeEvent); someObject.SomeEvent += someObject_SomeEvent; 
+6
source share
2 answers

Both are exactly the same. If you use the second (shorter) form, the compiler simply inserts a delegate type for you.

There is no reason to use the syntax that the delegate explicitly creates if you do not want the delegate type to exist in the code (for readability, etc.).

+10
source

A new, shorter version of delegate creation was introduced with the vs2003 compiler, as I recall. It is just syntactic sugar in a longer version.

+4
source

Source: https://habr.com/ru/post/943889/


All Articles