What is the difference between different ways of attaching / detaching event handlers in C #

My question is two parts -

First, we can attach event handlers in two ways:

myObject.MyEvent += new EventHandler(MyHandler);

myObject.MyEvent += MyHandler;

In my understanding, these two are equivalent. In the second case, the C # compiler performs the task of creating an instance of the delegate from the corresponding overload from the specified method group. It's right?

Secondly, is there a difference between the two corresponding handler detach styles? If so, what is it?

 myObject.MyEvent -= new EventHandler(MyHandler);

 myObject.MyEvent -= MyHandler;
+3
source share
2 answers

There is no difference in the IL code that was generated, as you mentioned. In any case, the C # compiler creates a handler.

There is also no difference when deleting.

+4

, # 1.2, .

+5

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


All Articles