-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFunctionPointer.cs
More file actions
27 lines (22 loc) · 876 Bytes
/
FunctionPointer.cs
File metadata and controls
27 lines (22 loc) · 876 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void Main()
{
unsafe
{
int outputUnsafe = CalculateUnsafe(&Sum, 3, 4); // calli IL instruction
// output -> 7
}
int outputDemo = Demo(Sum, 5, 6);
// output -> 11
}
static int Sum(int x, int y) => x + y;
//C# provides delegate types to define safe function pointer objects.
//Invoking a delegate involves instantiating a type derived from System.Delegate
//and making a virtual method call to its Invoke method.
//This virtual call uses the callvirt IL instruction.
//In performance critical code paths, using the calli IL instruction is more efficient.
static T Demo<T>(Func<T, T, T> calculate, T x, T y) =>
calculate(x, y);
//You can define a function pointer using the delegate* syntax.
//delegate* -> calli IL instruction , Func -> callvirt IL instruction
static unsafe T CalculateUnsafe<T>(delegate*<T, T, T> calculate, T x, T y) =>
calculate(x, y);