C# > Interop and Unsafe Code > Unsafe Code > unsafe Keyword
Direct Memory Manipulation with the `unsafe` Keyword
This example demonstrates how to use the `unsafe` keyword in C# to directly manipulate memory using pointers. While C# is generally a type-safe language, the `unsafe` keyword allows you to bypass certain safety checks for performance-critical operations, enabling direct access to memory addresses.
Introduction to `unsafe` Code
The `unsafe` keyword in C# allows you to write code that bypasses the type safety normally enforced by the Common Language Runtime (CLR). This means you can work directly with memory addresses using pointers. While this offers significant performance benefits in specific scenarios, it also introduces risks, such as memory corruption, buffer overflows, and access violations. Use `unsafe` code judiciously and only when necessary.
Basic `unsafe` Block
This code snippet demonstrates a basic `unsafe` block. First, we declare an integer variable `number`. Then, inside an `unsafe` block, we declare an integer pointer `pointerToNumber` and assign it the memory address of `number` using the `&` operator. Finally, we dereference the pointer using the `*` operator to modify the value stored at that memory address. The `((long)pointerToNumber).ToString("X")` converts the pointer to a long and then to a hexadecimal string for display.
using System;
public class UnsafeExample
{
public static unsafe void Main(string[] args)
{
int number = 10;
int* pointerToNumber = &number; // Get the address of 'number'
Console.WriteLine("Original value: " + number);
Console.WriteLine("Address of number: 0x" + ((long)pointerToNumber).ToString("X"));
*pointerToNumber = 20; // Modify the value at the memory address
Console.WriteLine("Modified value: " + number);
}
}
Concepts Behind the Snippet
Real-Life Use Case: High-Performance Image Processing
In image processing, manipulating pixel data directly can significantly improve performance. Instead of accessing individual pixels through array indexing (which involves bounds checking), `unsafe` code allows you to treat the image data as a contiguous block of memory and use pointers to iterate through the pixels. This can lead to substantial speed gains, especially for large images. However, it requires careful handling to avoid out-of-bounds access.
Best Practices
Interview Tip
When discussing `unsafe` code in an interview, emphasize that it should be used as a last resort and only when the performance benefits outweigh the risks. Be prepared to discuss the potential dangers and mitigation strategies.
When to Use Them
Use `unsafe` code in situations where performance is critical and managed code limitations become a bottleneck. Common scenarios include:
Memory Footprint
Using `unsafe` code doesn't inherently change the memory footprint. The data structures you're working with still occupy the same amount of memory. The main difference is how you access that memory: directly via pointers instead of managed references. However, incorrect pointer arithmetic can lead to writing to unintended memory locations, which can indirectly corrupt other data structures and lead to unpredictable behavior. It's paramount to fully understand the memory layout you're working with.
Alternatives
Before resorting to `unsafe` code, consider these alternatives:
Pros
Cons
FAQ
-
What is the `/unsafe` compiler option?
The `/unsafe` compiler option is required to compile C# code that uses the `unsafe` keyword. It tells the compiler to allow code that performs direct memory manipulation. -
Can I use `unsafe` code in a web application?
Yes, you can use `unsafe` code in a web application, but you should be extremely careful due to the security implications. Ensure that you thoroughly validate all input and output and minimize the scope of the `unsafe` blocks. -
How does garbage collection work with `unsafe` code?
The garbage collector (GC) is generally unaware of the memory manipulated by `unsafe` code. Therefore, it's your responsibility to ensure that you don't create memory leaks or dangling pointers. Consider using the `GC.KeepAlive` method to prevent the GC from prematurely collecting objects that are being used by `unsafe` code.