简体   繁体   English

C#中的事件或Lambda?

[英]Events or Lambdas in C#?

I'm throwing this out there just as a question of curiosity... 我把它扔出去只是好奇心的问题......

Assuming you're only expecting/wanting one method to be provided , would this be frowned upon or bad practice? 假设你只是期望/想要提供一种方法 ,这是不赞成还是不好的做法?

public class Something  {
    public Action OnRemove = () => { };
    public Action<object, EventArgs> OnFinishedLoading = (sender, e) => { };
}

// then used like...
something.OnRemove = () => { /*do something...*/ };
something.OnFinishedLoading = (sender, e) => { /*do something else...*/ };

I realize this is like cheating at events, but I is there anything that is bad about this approach? 我意识到这就像在事件中作弊一样,但我对这种方法有什么不好的意思吗? Would this cause any potential issues with your application in the long run? 从长远来看,这是否会导致您的应用程序出现任何潜在问题?

I realize if you're wanting more than one method to run, then an event would be better, this is mainly a question of if you're only wanting/expecting one method. 我意识到如果你想要运行多种方法,那么事件会更好,这主要是一个问题,即你是否只想要/期望一种方法。

Well, the most obvious thing "wrong" with it is that it's exposing public fields. 嗯,最明显的“错误”是它揭露了公共领域。 I'd at least use a property - at which point an event would probably be simpler anyway. 我至少使用了一个属性 - 此时事件可能会更简单。 It gives more flexibility with less code than a property (because you couldn't use an auto-implemented property with a specific default value). 它使用比属性更少的代码提供更大的灵活性(因为您无法使用具有特定默认值的自动实现的属性)。

The other alternative would be to take it in the constructor and then keep it as a private read-only field. 另一种方法是将它放在构造函数中,然后将其保存为私有只读字段。 That's probably what I'd really be inclined to do... is there really any need to expose the action as a property/field at all? 这可能是我真正倾向于做的事情......是否真的需要将行动作为财产/领域公开?

There is nothing wrong with this approach and I use it very often. 这种方法没有任何问题,我经常使用它。 It makes simple handlers have simple code (which is a good thing). 它使简单的处理程序具有简单的代码(这是一件好事)。

The only downside to this approach is that a handler created in this way cannot be easily removed. 这种方法的唯一缺点是以这种方式创建的处理程序不能轻易删除。 Normally you cane say 通常你会说

something.OnRemove -= SomeEvent;

But this is not the case with all lambda expressions. 但是所有lambda表达式都不是这种情况。 Textually equal lambda expressions will almost certainly have separate implementations and hence will not match up for the purpose of adding and removing event handlers. 文本上相等的lambda表达式几乎肯定会有单独的实现,因此不会匹配添加和删除事件处理程序的目的。

For instance, the following code will fail to remove the handler. 例如,以下代码将无法删除处理程序。

something.OnRemove += () { MessageBox.Show("Removed!"); }
something.OnRemove -= () { MessageBox.Show("Removed!"); }

But as long as you only want to add, this is not a problem. 但只要您只想添加,这不是问题。

It potentially exposes more than you want to. 它可能会暴露出比你想要的更多。 Normally, an event does not permit a client to examine the contents of the MulticastDelegate that backs the event. 通常,事件不允许客户端检查支持事件的MulticastDelegate的内容。

我认为这种方法很好,它只是一种声明委托的简便方法(看看Action系列的定义)。

With the event, you can't do this: 有了这个事件,你不能这样做:

something.OnRemove = null;

The events gives you a wrapper, kind of the wrapper with properties and attributes, so you can ensure encapsulation over the Delegate Method. 这些事件为您提供了一个包装器,一种包含属性和属性的包装器,因此您可以确保对Delegate方法进行封装。

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

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