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

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

问题描述

有一个问题已经回答,这是在C#,不是已经使用事件实现的观察者模式?

There is a question already answered which is In C#, isn't 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.


  • 事件可以是界面的一部分,而
    委托字段不能

  • 外部类不能调用事件
    但常规代理可以

  • 事件有其他访问器(添加和删除)您可以覆盖并提供自定义功能

编辑:这是一个很好的写作与事件和代理之间的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天全站免登陆