调用Lambda函数和在Dispatcher中显式调用委托有什么区别?换句话说,以下内容之间有什么区别:
Dispatcher.BeginInvoke(new Action( () => Foo() ));
Dispatcher.BeginInvoke((Action)delegate () { Foo(); });
private void Foo() { }
答案 0 :(得分:3)
在这两种情况下,您都定义了一个匿名方法来调用foo();
注意:可以这样做
Dispatcher.BeginInvoke(Foo);
答案 1 :(得分:3)
没有区别。这两行代码都生成相同的MSIL code
(例如ILSpy
所示):
IL_000f: ldarg.0
IL_0010: call instance class [WindowsBase]System.Windows.Threading.Dispatcher [WindowsBase]System.Windows.Threading.DispatcherObject::get_Dispatcher()
IL_0015: ldarg.0
IL_0016: ldftn instance void WpfApplication2.MainWindow::'<.ctor>b__0_0'()
IL_001c: newobj instance void [mscorlib]System.Action::.ctor(object, native int)
IL_0021: ldc.i4.0
IL_0022: newarr [mscorlib]System.Object
IL_0027: callvirt instance class [WindowsBase]System.Windows.Threading.DispatcherOperation [WindowsBase]System.Windows.Threading.Dispatcher::BeginInvoke(class [mscorlib]System.Delegate, object[])