删除冗余委托构造函数调用? [英] Remove redundant delegate constructor call?

查看:133
本文介绍了删除冗余委托构造函数调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下载了ReSharper,它告诉我改变这一行:

I downloaded ReSharper and it is telling me to change this line:

dispMap.OnDraw += new EventHandler(dispMap_OnDraw);

要成为这一行:

dispMap.OnDraw += dispMap_OnDraw;

因为第一行是冗余委托构造函数调用。

Because the first line is a "redundant delegate constructor call."

这是真的吗?在自动生成的表单设计器代码中,语法基于第一段代码,当输入 dispMap.OnDraw + = 并点击TAB时,IDE将自动生成 new EventHandler(dispMap_OnDraw)

Is this true? In the automatically generated designer code for forms the syntax is based on the first piece of code and when typing in dispMap.OnDraw += and hitting TAB the IDE automatically generates new EventHandler(dispMap_OnDraw)

我只是好奇这个。 ReSharper有一点吗?

I'm just curious about this one. Does ReSharper have a point?

推荐答案

是的,这是正确的。我在几种情况下做到了这一点。

Yes, this is correct. I have done this in several cases.

委托构造函数调用应该是隐式的;该类型可以从 OnDraw 推断,并根据 dispMap_OnDraw 的方法签名进行验证。

The delegate constructor call should be implicit; the type can be inferred from OnDraw and validated against the method signature of dispMap_OnDraw.

另外,此MSDN文章似乎相关:


因为+ =运算符只是将一个代理的内部调用
列表连接到另一个代理可以使用+ =添加
匿名方法。请注意,使用匿名事件处理,您不能
使用 - =运算符删除事件处理方法,除非
匿名方法作为处理程序添加,首先将其存储到
委托,然后注册该委托与事件。

Because the += operator merely concatenates the internal invocation list of one delegate to another, you can use the += to add an anonymous method. Note that with anonymous event handling, you cannot remove the event handling method using the -= operator unless the anonymous method was added as a handler by first storing it to a delegate and then registering that delegate with the event.

我相信委托实例是以任一方式创建的,但由于您没有对象引用当您隐式实例化代理时,您无法使用 - = 运算符将其删除。

I believe the delegate instance is created either way, but since you don't have an object reference for the delegate when you implicitly instantiate, you can't remove it with the -= operator.

这篇关于删除冗余委托构造函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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