Skip to content

Commit e5cfd53

Browse files
Use 'unsafe' explicitly to have it in snippets (#44573)
1 parent 55a932f commit e5cfd53

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed
Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace UnsafeCodePointers;
22

3-
public unsafe class FunctionPointers
3+
public class FunctionPointers
44
{
55
public static void PointerExamples()
66
{
@@ -10,35 +10,36 @@ public static void PointerExamples()
1010
Console.WriteLine();
1111

1212
// <InvokeViaFunctionPointer>
13-
static int localMultiply(int x, int y) => x * y;
14-
int product = UnsafeCombine(&localMultiply, 3, 4);
13+
int product = 0;
14+
unsafe
15+
{
16+
static int localMultiply(int x, int y) => x * y;
17+
product = UnsafeCombine(&localMultiply, 3, 4);
18+
}
1519
// </InvokeViaFunctionPointer>
1620
Console.WriteLine(product);
17-
18-
1921
}
2022

2123
// <UseDelegateOrPointer>
2224
public static T Combine<T>(Func<T, T, T> combinator, T left, T right) =>
2325
combinator(left, right);
2426

25-
public static T UnsafeCombine<T>(delegate*<T, T, T> combinator, T left, T right) =>
27+
public static unsafe T UnsafeCombine<T>(delegate*<T, T, T> combinator, T left, T right) =>
2628
combinator(left, right);
2729
// </UseDelegateOrPointer>
2830

2931
// <UnmanagedFunctionPointers>
30-
public static T ManagedCombine<T>(delegate* managed<T, T, T> combinator, T left, T right) =>
32+
public static unsafe T ManagedCombine<T>(delegate* managed<T, T, T> combinator, T left, T right) =>
3133
combinator(left, right);
32-
public static T CDeclCombine<T>(delegate* unmanaged[Cdecl]<T, T, T> combinator, T left, T right) =>
34+
public static unsafe T CDeclCombine<T>(delegate* unmanaged[Cdecl]<T, T, T> combinator, T left, T right) =>
3335
combinator(left, right);
34-
public static T StdcallCombine<T>(delegate* unmanaged[Stdcall]<T, T, T> combinator, T left, T right) =>
36+
public static unsafe T StdcallCombine<T>(delegate* unmanaged[Stdcall]<T, T, T> combinator, T left, T right) =>
3537
combinator(left, right);
36-
public static T FastcallCombine<T>(delegate* unmanaged[Fastcall]<T, T, T> combinator, T left, T right) =>
38+
public static unsafe T FastcallCombine<T>(delegate* unmanaged[Fastcall]<T, T, T> combinator, T left, T right) =>
3739
combinator(left, right);
38-
public static T ThiscallCombine<T>(delegate* unmanaged[Thiscall]<T, T, T> combinator, T left, T right) =>
40+
public static unsafe T ThiscallCombine<T>(delegate* unmanaged[Thiscall]<T, T, T> combinator, T left, T right) =>
3941
combinator(left, right);
40-
public static T UnmanagedCombine<T>(delegate* unmanaged<T, T, T> combinator, T left, T right) =>
42+
public static unsafe T UnmanagedCombine<T>(delegate* unmanaged<T, T, T> combinator, T left, T right) =>
4143
combinator(left, right);
4244
// </UnmanagedFunctionPointers>
43-
4445
}

0 commit comments

Comments
 (0)