What is the difference between a delegate instance and a method pointer?

前端 未结 3 900
执念已碎
执念已碎 2021-02-08 20:29

I thought that a delegate instance was interchangeable with a function instance.

Take the following code:

delegate int AddDelegate(int a, int b);

AddDel         


        
相关标签:
3条回答
  • 2021-02-08 21:07

    Terminology Corretion: Instead of method pointer, the more appropriate term is method group.

    In terms of functionality the two statements are equivalent. That is that they produce almost the same IL. The difference is where the delegate value is stored.

    In the first case you pass the method group Add to MethodThatTakesAdd directly. This causes a temporary delegate value to be created and then passed to MethodThatTakesAdd. This delegate value is subject to garbage collection the moment the MethodThatTakesAdd returns since it does not store the value.

    In the second case you assigned the delegate to a field on the outer instance. This will typically increase the lifetime of the delegate and hence reduce the chance it's garbage collected during your pinvoke call.

    0 讨论(0)
  • 2021-02-08 21:07

    Delegates are classes that are callable, and have similar behavior to function pointers. The delegate internally stores the address of the function to call (i.e. the function pointer), but also provides other functionality such as multi-casting and storing an invocation list; you can essentially invoke many functions of the same signature with one delegate instance as follows.

    public void DoStuff()
    {
        DelegateInstance += Add;
        DelegateInstance += AnotherAdd;
        DelegateInstance += YetAnotherAdd;
    
        // Invoke Add(100, 200), AnotherAdd(100, 200), and YetAnotherAdd(100, 200)
        DelegateInstance(100, 200);
    }
    

    Regarding your note about the equivalence of MethodThatTakesAdd(Add) and MethodThatTakesAdd(DelegateInstance), if you look at the MSIL that the C# compiler generates for the line MethodThatTakesAdd(Add), you will notice that the compiler is creating a delegate and wrapping the Add() method for you.

    0 讨论(0)
  • 2021-02-08 21:18

    While delegates provide synonymous functionality in C# as function pointers in C or C++, there are significant differences. Key among these is that a delegate is a class, not a pointer.

    In short, casting a delegate to a pointer isn't going to give you a reference to a function or method, and as such it can't be used to call a method by reference from unmanaged code.

    0 讨论(0)
提交回复
热议问题