它们是 100% 相同的 - 这是纯粹的编译器糖(见下文)。至于哪个是首选:都不是/两者。
static class Program {
static void Main()
{
method1 objDel2;
objDel2 = new method1(TestMethod1);
objDel2("test");
objDel2.Invoke("Invoke");
}
delegate void method1(string val);
static void TestMethod1(string val) {
System.Console.WriteLine(val);
}
}
有 IL
.method private hidebysig static void Main() cil managed
{
.entrypoint
.maxstack 2
.locals init (
[0] class Program/method1 'method')
L_0000: ldnull
L_0001: ldftn void Program::TestMethod1(string)
L_0007: newobj instance void Program/method1::.ctor(object, native int)
L_000c: stloc.0
L_000d: ldloc.0
L_000e: ldstr "test"
L_0013: callvirt instance void Program/method1::Invoke(string) ***HERE***
L_0018: ldloc.0
L_0019: ldstr "Invoke"
L_001e: callvirt instance void Program/method1::Invoke(string) ***HERE***
L_0023: ret
}
注意两者都做同样的事情(见我标记为的两个位置***HERE***
)