【问题标题】:Delegate add already handling event. Delegate remove not handling event委托添加已经处理的事件。委托移除不处理事件
【发布时间】:2019-05-16 09:31:14
【问题描述】:

我有一个与 C# 中的委托有关的问题。

  1. 如果我对已经在右侧处理事件的委托使用 += 会发生什么? 例如:

     MouseDown += new MouseEventHandler(ShootOnMouseDown);
     MouseDown += new MouseEventHandler(ShootOnMouseDown);
    

第二行会发生什么?

  1. 如果我在未处理右侧事件的委托上使用 -= 会发生什么? 前任: MouseDown 没有处理 ShootOnMouseDown,那么:

     MouseDown -= new MouseEventHandler(ShootOnMouseDown);
    

然后会发生什么?

非常感谢!

【问题讨论】:

  • 为什么不写一些代码自己试试呢?
  • 是的,我试过了,但在我看来什么都没发生,所以...:D

标签: c# events delegates


【解决方案1】:

1- ShootOnMouseDown 方法将被调用两次。

2- 什么都没有发生。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多