简体   繁体   English

C#为什么我在订阅活动时会使用“new”关键字?

[英]C# why shall I use “new” keyword when subscribing for an event?

What is the difference between following 2 ways of subscribing for an event? 订阅2种订阅方式有什么区别?

receiver.ConfigChanged += Config_ConfigChanged;

receiver.ConfigChanged += new EventHandler(Config_ConfigChanged);

It seems that both of them work the same way but if so, what is the point of using the second one? 似乎它们都以相同的方式工作,但如果是这样,那么使用第二种方法有什么意义呢?

What about unsubscribing, will both following methods work also the same way? 如果取消订阅,以下两种方法的工作方式是否相同?

receiver.ConfigChanged -= Config_ConfigChanged;

receiver.ConfigChanged -= new EventHandler(Config_ConfigChanged);

The verbose way works in all versions of C#, the short way only in C# 2 and later. 详细的方式适用于所有版本的C#,这只是C#2及更高版本的简短方法。 So I see no reason to use the long way nowadays. 所以我认为现在没有理由使用漫长的道路。

There are some situations where you still need to use new DelegateType(methodGroup) , but event subscribing isn't one of them. 在某些情况下,您仍然需要使用new DelegateType(methodGroup) ,但事件订阅不是其中之一。 These situations usually involve generic type inference or method overloading. 这些情况通常涉及泛型类型推断或方法重载。

Unsubscribing will work either way since it is based on value equality, not referential equality. 取消订阅将以任何方式工作,因为它基于值相等而不是引用相等。 If I recall correctly both the implicit conversion from a method group and the explicit new get translated to the same IL code. 如果我没记错,从方法组隐式转换和显式new get转换为相同的IL代码。 The implicit conversion is just syntax sugar. 隐式转换只是语法糖。

Visual Studio's TAB event auto-complete always defaults to the .Net 1.0 way of doing things regardless of what edition of the framework you are using. 无论您使用的是哪种版本的框架,Visual Studio的TAB事件自动完成始终默认为.Net 1.0的处理方式。 You may find some people who are used to reading the older way of doing things. 你可能会发现一些习惯于阅读旧方式的人。 I only came across the less verbose way through using Resharper! 通过使用Resharper,我只是遇到了那种不那么冗长的方式!

Here's an MSDN article on event subscription - it says exactly what @CodeInChaos just confirmed: http://msdn.microsoft.com/en-us/library/ms366768%28v=vs.80%29.aspx 这是一篇关于事件订阅的MSDN文章 - 它确切地说明了@CodeInChaos刚刚确认的内容: http//msdn.microsoft.com/en-us/library/ms366768%28v=vs.80%29.aspx

Correct me if i'm wrong, but i don't know if this works 如果我错了,请纠正我,但我不知道这是否有效

receiver.ConfigChanged += new EventHandler(Config_ConfigChanged);
receiver.ConfigChanged -= new EventHandler(Config_ConfigChanged);

Since these are 2 different instances I think this would 由于这些是两个不同的实例,我认为这样做

var configChanged = new EventHandler(Config_ConfigChanged);
receiver.ConfigChanged += configChanged;
receiver.ConfigChanged -= configChanged;

But then again, why not just use 但话又说回来,为什么不用

receiver.ConfigChanged += Config_ConfigChanged;
receiver.ConfigChanged -= Config_ConfigChanged;

Ok so 好的

receiver.ConfigChanged -= Config_ConfigChanged;

will clear out all event handlers that refer to that method. 将清除所有引用该方法的事件处理程序。

var eventHandler = new EventHandler(Config_ConfigChanged); 

receiver.ConfigChanged += eventHandler;
receiver.ConfigChanged += new EventHandler(Config_ConfigChanged); 

receiver -= eventHandler;

will only clear out the one eventHandler. 只会清除一个eventHandler。

You use the verbose way if you don't care about tracking the handler . 如果您不关心跟踪处理程序,则使用详细的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM