Any difference is there Invoke Method (Delegate) and direct call? [duplicate]

They are 100% identical – this is pure compiler sugar (see below). As for which is preferred: neither / both.

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);
    }
}

has the 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 
}

Note both do the same thing (see the two locations marked by me as ***HERE***)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)