可能重复: 我应该创建一个新的代理实例吗?
我已经尝试搜索这个答案,但是真的不知道要搜索什么字词,而且没有一个网站提出的问题是相关的。我确定这一定已经被解决了。
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)之间有什么区别?
发布评论