C#:SomeEvent + = Method和SomeEvent + = new Delegate(Method)之间有什么区别?

编程入门 行业动态 更新时间:2024-10-26 16:21:34
本文介绍了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)之间有什么区别?

本文发布于:2023-11-07 04:14:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565551.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   SomeEvent   Method   Delegate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!