Unsafe Code

Unsafe code, as defined in the next section, permits the use of pointers, which supports certain performance optimizations and interface to legacy code and operating systems. Unsafe code is identified with a special keyword, unsafe, which marks either a block of code or a field. This establishes an unsafe context where pointer operations can be implemented.

There are special keywords associated with unsafe contexts, making it easier to work with pointers. The fixed keyword helps pin down objects in memory so the garbage collector doesn't move them in the middle of an operation. Obtaining the size of a pointer or field can be accomplished by using the sizeof operator. The stackalloc operator enables memory to be allocated on the stack. ...

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.