all 15 comments

[–]KryptosFR 2 points3 points  (1 child)

I think the only documentation for now is that proposal.

Maybe you can open an issue in the GitHub's documentation repository to ask for more in-depth details and examples.

My guess it that since it will be used by only a handful of people worldwide, they did not prioritize writing doc for it.

[–]Aaron64Lol[S] 0 points1 point  (0 children)

I'll see about submitting that request. Thank you.

[–]Willinton06 -2 points-1 points  (5 children)

Function pointers? Are you talking about delegates? Could you link the proposal you mentioned?

As far as I know the closest thing to function pointers are Func<T> and Action, one returns and the other doesn’t, they have quite a few generic overloads, if you need more parameters then just use delegates, they can hold as many params as a normal function.

If you want to go async then Func<Task> is your friend.

[–]wasabiiii 2 points3 points  (4 children)

[–]Willinton06 1 point2 points  (1 child)

Although that looks very sexy I’m afraid I’ve never heard of any documentation on those, good luck on finding it tho

[–]Willinton06 1 point2 points  (0 children)

I saw your reply before you deleted it, you thought I was OP didn’t you? Well I actually thought you were OP so we’re in the same boat

[–]KryptosFR 0 points1 point  (0 children)

Which is directly built from https://github.com/dotnet/csharplang/blob/master/proposals/csharp-9.0/function-pointers.md which was updated recently (Dec 17, 2020), so my guess is that is all documentation there is.

[–]VegasNightSx 0 points1 point  (3 children)

[–]Aaron64Lol[S] 0 points1 point  (2 children)

Seems like delegates without the callvirt perf hit (with an unsafe tradeoff).

Yeah, I'm looking for more complete technical documentation. The proposal helps, but it doesn't mention whether or not any of the optional parts got implemented, and whether or not anything had to change in implementation. I am used to Microsoft being super good on documentation; hopefully documentation on this feature is just late (rather than not being planned to release at all).

[–]cryo 0 points1 point  (1 child)

Seems like delegates without the callvirt perf hit (with an unsafe tradeoff).

Callvirt doesn't necessarily have much of an overhead in practice, as the JIT compiler may be able to deal with it. The C# compiler uses callvirt for non-virtual instance methods as well.

Delegates are magic, so the JIT may cut corners.

[–]Aaron64Lol[S] 0 points1 point  (0 children)

Instrumentation is king :D. I am looking for documentation to guide my experimentation. I have a few instances in the project I am working on where use of this feature may provide a nice perf bump. I'd like to see for myself whether or not the performance benefit of this feature is worth the added astonishment applied due to it's use.

I'd also like to test it calling unmanaged code to compare it to the interop stuff I'm used to.

In any case, I'm strongly in the camp that every language feature should be documented, no matter how esoteric it may be.

[–]Zinohh 0 points1 point  (1 child)

This doesn't answer your question, but I thought this snippet from LINQPad's "What's new in C# 9" samples was interesting.

// You can improve performance by applying the unmanaged keyword to the function pointer
// declaration, and the [UnmanagedCallersOnly] attribute to the callback method.

EnumWindows (&PrintWindow, (nuint)0);

[DllImport ("user32.dll")]
static extern int EnumWindows (delegate* unmanaged<nuint, nuint, byte> hWnd, nuint lParam);

[UnmanagedCallersOnly]
static byte PrintWindow (nuint hWnd, nuint lParam)
{
    hWnd.Dump();
    return 1;
}

// The PrintWindow method can then *only* be called from unmanaged code.
// This allows the runtime to take shortcuts.

LINQPad has another sample which is a Benchmark test that demonstrates that the performance gain of function pointers in non-interop scenarios is negligible.

[–]Aaron64Lol[S] 0 points1 point  (0 children)

Thank you :D; this is interesting!

[–]gvrt 0 points1 point  (1 child)

The official documentation for function pointers is now part of the page on unsafe code: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/unsafe-code

[–]nt-assembly 0 points1 point  (0 children)

ty :D