.net - 在 .NET 中,为什么事件连接顺序如此重要?

标签 .net events delegates

using System;

static class Program
{
    static event Action A = delegate { };
    static event Action B = delegate { };

    static void Main()
    {
        A += B;
        B += ()=>Console.WriteLine("yeah");
        A.Invoke();
    }
}

这不会打印任何内容,但是如果我交换 Main 的前两行,它会打印。

最佳答案

事件是不可变的,即在分配时您会得到一个副本,例如整数

int a = 1;
int b = 2;

a += b; // a == 3
b += 1; // a is still 3

关于.net - 在 .NET 中,为什么事件连接顺序如此重要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7738871/

相关文章:

.net - "Iron"语言准备好迎接黄金时段了吗?

c# - 在包装 PInvoke 东西的属性的 get 函数中放置大量代码是不好的做法吗?

c# - 检查对象是否有带有委托(delegate)签名的方法

ios - 将委托(delegate)对象类型作为协议(protocol)方法参数传递

c# - 如何正确等待异步委托(delegate)?

c# - 使用 WinSCP .NET 程序集检查 SFTP 服务器上是否存在任何具有扩展名的文件

c# - 为什么 StringComparison.InvariantCultureIgnoreCase 不适用于此 Db4o linq 查询?

c++ - 创建字符串到函数 vector 的 HashMap

javascript - 单击按钮清除对象属性

c++ - Win32中是否有C++跨平台 "named event like the "CreateEvent()”?