C# Reference ยท Keywords and Statements
unsafe and fixed
Reference for C# unsafe and fixed: enable pointer code, pin managed arrays and strings, use stackalloc and fixed buffers, and know the risks.
unsafe marks a type, member or block in which you may use pointer types (int*), take addresses with &, dereference with * and do pointer arithmetic. The compiler no longer proves memory safety there: reading past an array, or through a stale pointer, can corrupt memory or crash the process. The project must also allow unsafe code (the AllowUnsafeBlocks property, or -unsafe for csc).
fixed is used inside unsafe code. A fixed statement pins a managed object (an array, a string, or a field of an object) so the garbage collector cannot move it, and gives you a pointer that is valid only inside the statement. A fixed size buffer, declared in a struct, embeds a small array directly in the struct.
unsafe { int[] data = { 1, 2, 3 }; fixed (int* p = data) { p[1] = 20; } }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
unsafe { ... } // block unsafe void M() { ... } // method (also: type, field, property, local function) fixed (T* p = array) { ... } // pin an array (its first element) fixed (char* p = someString) { ... } // pin a string fixed (T* p = &obj.field) { ... } // pin the object that holds the field fixed (T* p = span) { ... } // any type with an accessible GetPinnableReference() fixed byte Name[16]; // fixed size buffer in a struct T* p = stackalloc T[n]; // stack memory
Pointer types: the element type must be unmanaged (primitives, enums, pointers, or structs that contain only unmanaged fields). void* is allowed; object* is not. nint/nuint are native-size integers and need no unsafe.
Errors / undefined behavior: an out-of-range pointer read or write, or using a fixed pointer after the statement ends, is undefined behavior. The runtime cannot detect it.
Usage notes
- Pinning duration: the pointer from
fixedis only valid inside the statement. Never store it. Pin for the shortest time possible, because pinned objects fragment the heap. - Empty arrays: for a
nullor zero-length array the pointer isnull, so check the length first. - Strings: a pinned string pointer reads as a null-terminated sequence of
char. Never write through it: strings are immutable and may be shared. - Alternatives first:
Span<T>,stackallocassigned to aSpan<T>(nounsafeneeded),MemoryMarshalandreflocals cover most performance needs safely. - Interop: pointers matter mostly for calling native code (P/Invoke, function pointers) and for low-level libraries.
- Fixed buffers only hold primitives, are limited by the struct that contains them, and since C# 11 can be used from safe code when the struct itself is accessible.
- No bounds checks, no GC tracking: one wrong index is a crash or corruption, not an exception.