简体   繁体   English

C#处理事件

[英]C# Handling Events

When I deal set up events, I usually write as such: 当我处理设置事件时,我通常这样写:

data.event += new data.returndataeventhandler(method);

And have a method as such: 并有这样一种方法:

void method(parameter)
{
   dosomething();
}

This is when the event returns an object. 这是事件返回一个对象的时间。

I have just been reading through somebody elses code and they have used, what seems to be a much cleaner way, as such: 我刚刚阅读了别人的代码,他们使用了似乎更简洁的方法,例如:

data.ReturnData += delegate(DataSet returnedDataSet)
                                    {
                                        dataset = returnedDataSet;
                                    };

Is there any downfall to this way? 这样有什么失败吗?

Thanks. 谢谢。

The one major downfall of using anonymous delegates (or the even-cleaner Lambda as suggested by tster) is that you're not going to be able to unsubscribe it from the event later unless you give it some sort of name. 使用匿名委托(或tster建议的更整洁的Lambda)的一个主要缺点是,除非您给它提供某种名称,否则以后您将无法取消订阅该事件。

In most cases, this is "No Big Deal (tm)" because the delegate will go away whenever the event source goes away, but this can be a "Subtle Mistake (tm)" if you're subscribing to static events or events on long-lived objects (eg, the WPF Dispatcher object). 在大多数情况下,这是“没什么大不了(tm)”,因为无论事件源何时消失,委托都会离开,但是如果您订阅静态事件或事件时,则可能是“微妙的错误(tm)”。长寿命的对象(例如WPF Dispatcher对象)。

In your case, this doesn't look like a problem at all, so I'd definitely recommend going with tster's recommendation (assuming you're using an appropriately recent version of .Net): 在您的情况下,这根本看起来不成问题,因此,我绝对建议您遵循tster的建议(假设您使用的是.Net的最新版本):

data.ReturnData += returnedDataSet => dataset = returnedDataSet;

(The compiler can infer the type of returnedDataSet from the EventHandler type of ReturnData .) (编译器可以推断出的类型returnedDataSet从事件处理程序类型的ReturnData 。)

The primary downfall of using anonymous delegates is that they are not reusable. 使用匿名委托的主要缺点是它们不可重用。 Other than that there is typically no difference between defining a delegate and then using it elsewhere in your code versus using an anonymous delegate. 除此之外,定义委托然后在代码的其他地方使用它与使用匿名委托之间通常没有区别。

One down fall is that it will not appear in your method drop down list. 一个失败的选择是它不会出现在您的方法下拉列表中。 If you do it inline, it should only be simple, nothing overly complex. 如果您以内联方式进行操作,那么它应该只是简单的,而不会太复杂。

Like said by others, the most obvious is not reusable. 就像别人说的那样,最明显的是不可重用的。 Other points: 其他要点:

  • readability in particular if you have large method body 可读性特别是如果您的方法主体很大
  • because .NET generate a random name for anonymous method (not very meaningful or readable) if you use reflection type technology or profiler, it may complicate traceability. 因为.NET如果使用反射类型技术或探查器,会为匿名方法生成一个随机名称(意义不大或可读性很差),所以它可能会使可追溯性复杂化。

The only downfall is that if you have more than one event it's easier to point it to a method. 唯一的缺点是,如果您有多个事件,则将其指向一种方法会更容易。 If you had to attach events in different blocks to the same handler, you would have to store your delegate somewhere so that both blocks could "see" it. 如果必须将不同块中的事件附加到同一处理程序,则必须将委托存储在某个位置,以便两个块都可以“看到”它。

甚至更清洁:

data.ReturnData += returnedDataSet => dataset = returnedDataSet;

Nope its just anonymous method thats all. 不,它只是匿名方法而已。 You can read more about anonymous methods here . 您可以在此处阅读有关匿名方法的更多信息。

Aside from the other answers of reusablity/Intellisense, I believe the only downfall is if you need to remove the handler later. 除了重用性/智能感知的其他答案外,我认为唯一的失败是如果您以后需要删除处理程序。 With a delegate/lamba you cannot easily remove your handler if it no longer needs to be called. 使用委托/ Lamba,如果不再需要调用处理程序,则无法轻松删除它。

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

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