How are delegates in C# better than function pointers in C/C++?

后端 未结 4 984
心在旅途
心在旅途 2020-12-17 15:46

The delegates in C# offer similar functionality as function pointers in C. I heard someone saying \"C# delegates are actually better than function pointers in C\". How come?

4条回答
  •  再見小時候
    2020-12-17 16:19

    One thing that a delegate provides that a C/C++ function pointer doesn't is type safety. That is, in C/C++, you can shove a function pointer into a function pointer variable declared with the wrong function signature (or even an int a double or worse with appropriate coaxing), and the compiler will be happy to produce code that calls the function completely incorrectly. In C#, the type signature of the function must match the type signature of the delegate and also the way the delegate is ultimately called.

提交回复
热议问题