活动和代表及其相应的应用程​​序之间的差异 [英] Difference between events and delegates and its respective applications

查看:99
本文介绍了活动和代表及其相应的应用程​​序之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有看到过使用委托的事件,不是被语法糖等优点。也许我误解,但似乎该事件仅仅是代表一个占位符。

I don't see advantages of using events over delegates, other than being syntactical sugar. Perhaps I am misunderstanding, but it seems that event is just a placeholder for delegate.

你给我解释的差异,以及何时使用的?有哪些优势和劣势?我们code在很大程度上植根的事件,我想要得到它的底部。

Would you explain to me the differences and when to use which? What are the advantages and disadvantages? Our code is heavily rooted with events, and I want to get to the bottom of it.

当你使用过的事件的代表,反之亦然?请注明既你的现实世界的经验,比如在生产code。

When would you use delegates over events and vice versa? Please state your real world experience with both, say in the production code.

推荐答案

从技术角度来看,其他的答案已经解决了分歧。

From the technical standpoint, other answers have addressed the differences.

从语义学的角度看,事件是在满足一定条件下由对象提出的行动。例如,我的股票类有一个叫做极限属性,它提出了当股票价格达到限制的事件。此通知经由事件完成。是否有人真正关心这一事件,并订阅了它超出了主阶级的关注。

From a semantics perspective, events are actions raised by an object when certain conditions are met. For example, my Stock class has a property called Limit, and it raises an event when the stock prices reaches the Limit. This notification is done via an event. Whether anyone actually cares about this event and subscribes to it is beyond the concern of the owner class.

委托是一个更通用的术语来描述类似于C / C ++方面的指针的结构。在.net所有代表都是多路广播委托。从语义的角度来看,它们通常用作一种输入的。特别是,他们是执行策略模式一个完美的方式。例如,如果我要排序的对象列表,我的方法告诉实现如何比较两个对象提供一个比较的策略。

A delegate is a more generic term to describe a construct similar to a pointer in C/C++ terms. All delegates in .Net are multicast delegates. From a semantics perspective, they are generally used as a kind of input. In particular, they are a perfect way to implement the Strategy Pattern. For example, if I want to sort a List of objects, I can provide a Comparator strategy to the method to tell the implementation how to compare two objects.

我已经用在生产中code中的两种方法。当某些属性是满足我的数据对象万吨通知。最基本的例子,每当属性发生变化,一个PropertyChanged事件引发(参见INotifyPropertyChanged接口)。我已经使用在code代表提供转向特定对象到字符串的不同的策略。这个特定的例子是一个特定对象的类型将其显示给用户实现的美化了的ToString()列表。

I have used the two methods in production code. Tons of my data objects notify when certain properties are met. Most basic example, whenever a property changes, a PropertyChanged event is raised (see INotifyPropertyChanged interface). I have used delegates in code to provide different strategies of turning certain objects into string. This particular example was a glorified ToString() list of implementations for a particular object type to display it to users.

这篇关于活动和代表及其相应的应用程​​序之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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