观察者模式在C#实现与代表? [英] Observer pattern implemented in C# with delegates?

查看:291
本文介绍了观察者模式在C#实现与代表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个问题已经回答是 HTTP ://stackoverflow.com/questions/32034/in-c-isnt-the-observer-pattern-already-implemented-using-events

There is a question already answered which is http://stackoverflow.com/questions/32034/in-c-isnt-the-observer-pattern-already-implemented-using-events

据询问是否观察者模式中使用事件C#已经实施。

It asks if the observer pattern is already implemented in c# using events.

虽然我得到的事件和观察者模式,是不是观察者模式真的只是委托和事件是进一步落实?

While I get the events and observer pattern, isn't the observer pattern really just delegates and events is a further implementation?

推荐答案

您是正确的。事件仅仅是一些稍微不同功能的委托。所有观察者模式可以与代表实现无需接触事件关键字。

You are correct. An event is simply a delegate with some slightly different functionality. All of the observer pattern can be implemented with delegates without ever touching the event keyword.

您可能会感兴趣,然后在什么事件关键字实际上带来的表。

You may be interested then, in what the "event" keyword actually brings to the table.


  • 活动可以是一个接口的一部分,而
    定期委托字段不能

  • 事件不能被外部类,
    ,但经常代表们能

  • 活动援引其他访问(添加和删除)你可以覆盖和

编辑提供自定义的功能:的Here's事件和代表们之间的IL代码比较大的书面记录。 (提示:这几乎是相同的)

Here's a great writeup with IL code comparison between events and delegates. (Hint: it's pretty much the same).

这篇关于观察者模式在C#实现与代表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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