Skip to content

C# Feature โ€“ unsafe & fixed

The unsafe context permits pointer operations; fixed pins managed objects to stable addresses.

C# 1 (2002)

unsafe
{
int x = 42;
int* p = &x;
*p = 100;
}
  • Requires project setting in your .csproj:
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<!-- optionally: <AllowUnsafeBlocks>$(Configuration)==Release</AllowUnsafeBlocks> -->
</PropertyGroup>
  • Prefer safe alternatives unless necessary for interop or performance.