are there function pointers in c#?

2019-01-15 19:09发布

I am trying to learn some c# coding and wondering if the c++ concept of function pointers is included in c#. I see there are such things as delegates. Are they the same concept? or do they differ on a more fundamental level?

4条回答
Evening l夕情丶
2楼-- · 2019-01-15 19:12

As others have said, you'll want to use delegates for situations in C# that you would have used function pointers for in C++. Delegates are conceptually similar to function pointers but far more pleasant to use, since they encapsulate not just the function, but also the "receiving" object which will be the "this" of the invocation.

Note that the CLR does have the concept of function pointers. If you were to take a close look at how the C# compiler generates code that constructs delegates, you'd see that it makes a managed reference to a function and passes that to a delegate constructor. There is no language feature in C# that allows you to get at the "naked" function pointer and manipulate it directly.

However, since the concept does exist in the CLR, it is theoretically possible that a future version of C# could support function pointers as a first-class concept in unsafe code, just as we support data pointers in unsafe code. What we would do in that case is (1) track the signature of the function pointer as the pointer type, and (2) emit code that uses the "calli" (call through pointer indirection) CIL opcode.

This would increase efficiency in certain obscure interop scenarios where today basically you have to make the marshalling code jump through a lot of hoops, building up delegate instances solely so that the marshaller can get at the function pointer stored in them. If we could avoid requiring the expense of delegate construction and go straight to the function pointer then those now-rare interop scenarios could become less expensive.

However, I wouldn't hold my breath waiting for that feature if I were you. We've prototyped it and it works reasonably well, but I don't think the demand is there to warrant adding it to a general-purpose language like C# at this time.

查看更多
地球回转人心会变
3楼-- · 2019-01-15 19:27

Not in the classical C/C++ sense, no. But the concept is somewhat similar - .NET introduces the concept of delegates to handle situations where you need a variable to invoke a method upon. Delegates cannot be "twiddled" with as pointers can and there is type safety built-in.

If you use C-style function pointers "correctly" the concepts are similar. But there seems to be a lot of legacy code which does funny manipulations of the pointers to skirt around type-safety or what-not.

查看更多
太酷不给撩
4楼-- · 2019-01-15 19:28

Delegates are essentially function pointers, but with extra multicast capabilities built in. So you can assign several functions to the same delegate, and they will all be called in sequence when the delegate is called.

Delegates also have built-in asynchronous interfaces, and have co/contra variance when assigning new functions to a delegate (and, in .NET 4, when passing delegates around)

查看更多
霸刀☆藐视天下
5楼-- · 2019-01-15 19:30

A delegate is similar to a function pointer in some ways, but actually it is closer to an interface with only one function combined with a way to register handlers and a multicast dispatching mechanism.

So it's a lot more than a function pointer.

查看更多
登录 后发表回答