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

前端 未结 3 899
执念已碎
执念已碎 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: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.

提交回复
热议问题