C#:SomeEvent + = Method和SomeEvent + = new Delegate(Method)之间有什么区别? [英] C#: what's the difference between SomeEvent += Method and SomeEvent += new Delegate(Method)

查看:105
本文介绍了C#:SomeEvent + = Method和SomeEvent + = new Delegate(Method)之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

我应该创建一个新的代理实例吗?

我已经尝试搜索这个答案,但是真的不知道要搜索什么字词,而且没有一个网站提出的问题是相关的。我确定这一定已经被解决了。

Hi, I've tried searching for the answer to this, but don't really know what terms to search for, and none of the site-suggested questions are relevant. I'm sure this must have been answered before though.

基本上,有人可以告诉我这两行在C#中有什么区别:

Basically, can somebody tell me what's the difference between these two lines in C#:

SomeEvent += SomeMethod
SomeEvent += new SomeDelegate(SomeMethod)

例如:

DataContextChanged += App_DataContextChanged;
DataContextChanged += new DependencyPropertyChangedEventHandler(App_DataContextChanged);

他们似乎都做同样的事情。

They both seem to do the same thing.

推荐答案

他们是一样的。第二个变体只是第一个调用方法组转换的简写。

They are the same. The second variant is just a shorthand for the first called Method group conversion.

简而言之,编译器推测什么代理的类型是使用事件本身的委托类型。如果没有错误的话,这是在C#2.0中引入的。

Simply put, the compiler infers what the type of the delegate is by using the delegate type of the event itself. This was introduced in C#2.0 if I'm not mistaken.

这篇关于C#:SomeEvent + = Method和SomeEvent + = new Delegate(Method)之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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