gpt4 book ai didi

c# - 不同的委托(delegate)用途

转载 作者:行者123 更新时间:2023-11-30 15:12:21 29 4
gpt4 key购买 nike

Possible Duplicate:
What is the difference between a delegate and events?

Possible Duplicate:
Difference between events and delegates and its respective applications

(copied from this duplicate)

当我不得不发起一个事件时,我会这样做

public delegate void LogUserActivity(Guid orderGUID);
public event LogUserActivity ActivityLog;

即使这样也行

public delegate void LogUserActivity(Guid orderGUID);
public LogUserActivity ActivityLog;

这两个有什么区别

最佳答案

这里有三件事:

  • 声明委托(delegate)类型
  • 创建委托(delegate)类型的公共(public)变量
  • 创建委托(delegate)类型的公共(public)事件

该变量只是一个普通变量 - 任何人都可以从中读取、分配给它等。事件 只向外界公开订阅/取消订阅功能。您在此处显示的类似字段的事件 有效地具有“默认”订阅/取消订阅行为,存储在具有相同名称的字段中。在声明类中,您访问该字段;您可以在外部访问该事件。

我有一个 article about events和更详细解释的委托(delegate)。

编辑:要回答评论,您可以使用“无操作”处理程序轻松初始化类似字段的事件:

public event LogUserActivity ActivityLog = delegate{};

关于c# - 不同的委托(delegate)用途,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1362515/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com