是什么在C#中attaching\detaching事件处理程序的不同方式之间的区别 [英] What is the difference between different ways of attaching\detaching event handlers in C#

查看:166
本文介绍了是什么在C#中attaching\detaching事件处理程序的不同方式之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两部分,我的问题 -



首先,我们可以将事件处理程序在以下两个方面 -

  myObject.MyEvent + =新的EventHandler(MyHandler的); 

myObject.MyEvent + = MyHandler的;



按我的理解,这两个是等价的。在第二种情况下,C#编译器从指定的方法组相应的过载创建委托实例的工作。它是否正确?



其次,是有两个对应的样式分离的处理程序有什么区别? ?如果是的话又是什么

  myObject.MyEvent  -  =新的EventHandler(MyHandler的); 

myObject.MyEvent - = MyHandler的;


解决方案

有是生成的IL代码没有区别 - 正如你所提到。 C#编译器创建一个处理程序呢。



在删除也,没有任何区别。


There are two parts to my question -

Firstly we can attach event handlers in following two ways -

myObject.MyEvent += new EventHandler(MyHandler);

myObject.MyEvent += MyHandler;

As per my understanding these two are equivalent. In the second case the C# compiler does the job of creating a delegate instance from the appropriate overload from the specified method group. Is this correct?

Secondly, is there any difference between the two corresponding styles of detaching the handler? If yes then what is it?

 myObject.MyEvent -= new EventHandler(MyHandler);

 myObject.MyEvent -= MyHandler;

解决方案

There is no difference in the IL code that is generated - as you mentioned. C# compiler creates a handler anyway.

In the removing also, there is no difference.

这篇关于是什么在C#中attaching\detaching事件处理程序的不同方式之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆