【问题标题】:What is +=symbol called in Adding multiple delegates to a delegate object?将多个委托添加到委托对象中调用的 +=symbol 是什么?
【发布时间】:2010-08-19 10:21:29
【问题描述】:
public delegate void ExecuteCallback();

类刽子手 { 私有 ExecuteCallback _doExecute;

public void AddMultiple()
{
    // Add a delegate to MethodA
    // This will work even if _doExecute is currently null
    _doExecute += new Execute( MethodA );

    // Add a delegate to MethodB also
    _doExecute += new Execute( MethodB );

    // Add a delegate to MethodC also
    _doExecute += new Execute( MethodC );
}

public void MethodA()
{
    //...
}

public void MethodB()
{
    //...
}

public void MethodC()
{
    //...
}

}

【问题讨论】:

  • 你的问题到底是什么?

标签: asp.net delegates


【解决方案1】:

+= 通常称为“加法赋值运算符”,但在委托的上下文中...

+= 运算符也用于 指定将被调用的方法 响应事件;这样的方法 被称为事件处理程序。指某东西的用途 在这种情况下 += 运算符是 被称为订阅 事件。

取自 MSDN 文档here

How to: Subscribe to and Unsubscribe from Events

【讨论】:

  • 请注意,事件和委托的处理是完全不同的。
【解决方案2】:

这只是 C# 提供的用于调用 Delegate.Combine 的简写。它也适用于事件,它调用事件的订阅部分(例如,C# 事件声明中的 add {} 块)。

我认为组合没有特定的名称 - 它只是二进制 + 和 += 运算符,分别来自 C# 规范的 7.8.4 和 7.17.2 部分。

【讨论】:

    【解决方案3】:

    我可能会将其称为向委托/事件“注册”事件处理程序。视情况而定。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 2012-03-23
      • 1970-01-01
      • 2020-05-31
      相关资源
      最近更新 更多