Do functors have an equivalent in C#? [duplicate]

南楼画角 提交于 2019-12-06 00:20:50

C# has Func<,>, delegates and anonymous methods but aren't all of these pointers to a method?

No. Even C# delegates are classes, implemented by the compiler for you. These generated classes (for delegates) are derived from MulticastDelegate which in turn derives from Delegate.

In short, a delegate is a syntactic sugar for a class generated by compiler.

Both lambdas (Func<>, Action<>) and delegates (named as well as anonymous) are classes.

If you need a pointer to method (to pass it into unsafe code, for instance) you should use marshalling:

IntPtr pFunc = Marshal.GetFunctionPointerForDelegate(myDelegate);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!