本文告诉大家使用局部函数可能遇到的坑。
在以前,如果有一个事件public event EventHandler Foo
和一个函数private void Program_Foo(object sender, EventArgs e)
那么使用函数监听事件是很简单的,当然从事件取消函数也是很简单。请看下面代码
for (int i = 0; i < 100; i++) { Foo -= Program_Foo; Foo += Program_Foo; } Console.WriteLine(Foo.GetInvocationList().Length);
结果输出 1
因为GetInvocationList
是获得事件有多少监听,从上面代码看到,只有一个监听。
如果把函数修改为局部,请看代码
for (int i = 0; i < 100; i++) { Foo -= Program_Foo; Foo += Program_Foo; } Console.WriteLine(Foo.GetInvocationList().Length);
void Program_Foo(object sender, EventArgs e) {
}
现在他会输出什么?
看起来没有问题,但是如果再做出一些修改,请看下面代码
for (int i = 0; i < 100; i++) { F(); } Console.WriteLine(Foo.GetInvocationList().Length);
private static void F() { Foo -= Program_Foo; Foo += Program_Foo;
void Program_Foo(object sender, EventArgs e) {
} }
现在输出是什么?
还是 1
所以可以直接使用局部函数

原文链接: http://blog.lindexi.com/post/C-%E5%B1%80%E9%83%A8%E5%87%BD%E6%95%B0%E4%B8%8E%E4%BA%8B%E4%BB%B6
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。 欢迎转载、使用、重新发布,但务必保留文章署名 林德熙 (包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我 联系。