活动与代表 [英] Events vs. Delegates

查看:72
本文介绍了活动与代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我是C#的新手(具有很长的C ++经验)而且我有点困惑

事件的语言结构。对于那些我不能与代表们讨论的事件,我能做些什么呢?目前在我看来微软已经通过两个关键词开发了类似的功能。我知道

事件提供了更好的封装,因为底层代表是私有的,但

就是这一切?

-

问候

Burkhard

Hi,

I am new to C# (with long year experience in C++) and I am a bit confused by
the language construct of events. What is it I can do with events that I
cannot do with delegates? At the moment it seems to me that Microsoft has
developed similar functionality via two keywords. I do understand that an
event offers better encapsulation as the underlying delegate is private, but
is that all ?
--
Regards
Burkhard

推荐答案



" Burkhard" <卜****** @ discussions.microsoft.com>在消息中写道

新闻:DF ********************************** @ microsof t.com ...

"Burkhard" <Bu******@discussions.microsoft.com> wrote in message
news:DF**********************************@microsof t.com...


我是C#的新手(具有C ++的长年经验)而且我有点困惑
by br />事件的语言结构。对于我不能与代表做的事情,我能做些什么呢?目前在我看来微软已经通过两个关键词开发了类似的功能。我知道
事件提供了更好的封装,因为底层代表是私有的,
但是
就是这样吗?
Hi,

I am new to C# (with long year experience in C++) and I am a bit confused
by
the language construct of events. What is it I can do with events that I
cannot do with delegates? At the moment it seems to me that Microsoft has
developed similar functionality via two keywords. I do understand that an
event offers better encapsulation as the underlying delegate is private,
but
is that all ?




嗯封装真的是重要的部分。允许外部

客户清除或执行事件是不可接受的。


但是,事件和委托虽然看起来一样,但工作方式不同。

事件'基本上是一种属性,为外界提供一套标准的

访问者,添加,删除和可选地提升(尽管

规范允许任意访问者。)它们在元数据中也很突出,因为

事件而不仅仅是类型委托的字段,它确实有一些

语义价值。事件是您可以自由订阅的,而

代表则不提供此类定义。委托字段可以并且经常用于其他目的。


简而言之,事件是一个事件,而代表只是一个事件。 />



Well, the encapsulation is really the important part. Allowing outside
clients the ability to clear or execute events is just not acceptable.

However, event and delegate, while they look the same, work differently.
Event''s are basically a type of property that offers a standard set of
accessors to the outside world, add, remove, and optionally raise(although
the spec allows for arbitrary accessors.) They also stand out in metadata as
events and not just as a field of type delegate which does have some
semantic value. An event is something you can subscribe to freely, whereas a
delegate doesn''t offer that type of definition. Delegate fields can and are
used for other purposes fairly often.

Put simply, an event is an event, whereas a delegate only might be.


事件是字段(好吧,并不总是,但大多数情况下),代表是类。

事件只是容器。一个或多个代表。当发生事件

时,将调用为特定事件注册的所有代理。一个

委托是一个指向回调的函数指针。使用的功能

来处理这个事件。


他们完全不同,没有类似的功能,但是他们一起使用
所以举起一个事件会调用你的一个方法。


代表们从不私有(从不说永远不会)。他们将永远是公开的,因为你的代码需要能够创建它们的实例。


事情比我更复杂一点已经说过,但这个b $ b应该让你知道它们是不同的。活动和代表是

不比枚举和财产更相似。


" Burkhard" <卜****** @ discussions.microsoft.com>在消息中写道

新闻:DF ********************************** @ microsof t.com ...
Events are field (well, not always, but mostly), Delegates are classes.
Event are just "containers" for one or more delegates. When an event
happens, all the delegates registered for a particular event are called. A
delegate is a function pointer which points to the "callback" function used
to handle the event.

They are completely different and have no similar functionality, but they
are used together so that the raising of an event calls one of your methods.

Delegates are never private (never say never). They will most always be
public because your code needs to be able to create an instance of them.

Things are a little bit more complicated than what I have said, but this
should give you an idea that they are different. Events and delegates are
no more similar than an enum and a property.

"Burkhard" <Bu******@discussions.microsoft.com> wrote in message
news:DF**********************************@microsof t.com...


我是C#的新手(具有C ++的长年经验)而且我有点困惑
by br />事件的语言结构。对于我不能与代表做的事情,我能做些什么呢?目前在我看来微软已经通过两个关键词开发了类似的功能。我确实理解,
事件提供了更好的封装,因为底层代表是私有的,
但是
就是这样吗?
-
问候
Burkhard
Hi,

I am new to C# (with long year experience in C++) and I am a bit confused
by
the language construct of events. What is it I can do with events that I
cannot do with delegates? At the moment it seems to me that Microsoft has
developed similar functionality via two keywords. I do understand that an
event offers better encapsulation as the underlying delegate is private,
but
is that all ?
--
Regards
Burkhard



http://blog.monstuff.com/archives/000040.html


" Burkhard" <卜****** @ discussions.microsoft.com>在消息中写道

新闻:DF ********************************** @ microsof t.com ...
http://blog.monstuff.com/archives/000040.html

"Burkhard" <Bu******@discussions.microsoft.com> wrote in message
news:DF**********************************@microsof t.com...


我是C#的新手(具有C ++的长年经验)而且我有点困惑
by br />事件的语言结构。对于我不能与代表做的事情,我能做些什么呢?目前在我看来微软已经通过两个关键词开发了类似的功能。我确实理解,
事件提供了更好的封装,因为底层代表是私有的,
但是
就是这样吗?
-
问候
Burkhard
Hi,

I am new to C# (with long year experience in C++) and I am a bit confused
by
the language construct of events. What is it I can do with events that I
cannot do with delegates? At the moment it seems to me that Microsoft has
developed similar functionality via two keywords. I do understand that an
event offers better encapsulation as the underlying delegate is private,
but
is that all ?
--
Regards
Burkhard



这篇关于活动与代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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