Summary

Unsafe code allows you to use pointers, a low-level mechanism designed to help optimize some routines. Pointers operate by holding the address of objects, providing indirect access to an object's value and member-wise access to structs. Pointers also may be manipulated arithmetically.

There are a few keywords that assist working with unsafe code. The sizeof() operator returns the number of bytes in a variable. Memory allocation is performed with the stackalloc operator. Since there's no guarantee that a moveable object in memory will stay in place, the fixed statement is used to pin a movable object in memory.

Platform Invoke—PInvoke—is a capability that allows C# programs to call native code libraries. To use PInvoke, methods are decorated ...

Get C# Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.