Working with unsafe code
The unsafe
keyword denotes a section of code that is not managed by the Common Language Runtime (CLR) or by unmanaged code. Unsafe
is used to declare a type or member or specify a block code. When used to qualify a method, the context of the entire method is unsafe.
We will mention managed and unmanaged code several times while discussing low-level programming and unsafe code. As a reminder, managed code executes under the supervision of the CLR and the Garbage Collector (GC). They perform housekeeping tasks such as the following:
- Managing memory for objects
- Performing type verification
- Doing garbage collection
Managed code in .NET is generally considered verifiably safe code, meaning that the .NET development tools can verify that the code is safe. The primary attribute of safe code is that it doesn’t directly access memory using pointers, allocate raw memory, or create managed objects.
On the other hand, unmanaged code...