C# and .NET examples · Memory and runtime
Unsafe.As in C#: Memory Reinterpretation Explained
A .NET 8 console application demonstrating System.Runtime.CompilerServices.Unsafe.As<TFrom, TTo>() — a zero-copy way to reinterpret memory in C#, treating the bytes of one type as another without any data conversion or allocation.
In this guide: Get started · Walk through the example · Understand the details · Check the limits
🚀 Quick start
Build and Run
cd UnsafeAs
dotnet run
📦 Prerequisites and installation
Requirements
💡 Usage
What Is Unsafe.As?
Unsafe.As<TFrom, TTo>(ref TFrom source) returns a managed reference (ref TTo) that points to the same memory as the input. It returns a reference to the existing storage without performing a numeric conversion or copying the value. The project enables <AllowUnsafeBlocks>true</AllowUnsafeBlocks> for the unsafe pointer arithmetic in Example 1.
Examples
Program.cs contains four numbered examples; the first two (reading and then modifying a long through an int reference) are shown together below.
Example 1 — long reinterpreted as int
Reinterprets a long value as a reference to its first 4 bytes as int. Modifying through the int reference updates the original long value’s low 32 bits.
Original long value: 42
As int (first 4 bytes): 42
As int (second 4 bytes): 0
After modifying through int reference, long value: 100
Example 2 — PointFloat reinterpreted as PointInt
Reinterprets a struct PointFloat { float X; float Y; } as PointInt { int X; int Y; }. The displayed integer values are the raw IEEE 754 bit patterns of the float values.
Original: (1, 2)
As PointInt: (1065353216, 1073741824)
Example 3 — byte[] reinterpreted as long
Gets a ref long to the first element of a byte[8] array and reads or writes all 8 bytes at once through a single 64-bit write.
Original bytes: [01, 02, 03, 04, 05, 06, 07, 08]
As long: 578437695752307201
After modification: [01, 02, 03, 04, 05, 06, 07, 08]
The bytes look unchanged after the modification because the code writes 0x0807060504030201, which is the same value: on a little-endian CPU (x86/x64 and Arm64 on Windows), the lowest byte 01 is stored first. Writing a different value would visibly change the array.
🏗️ Architecture
Project Structure
UnsafeAs/
├── Program.cs # Four Unsafe.As examples (shown as three sections above)
├── PointFloat.cs # struct PointFloat { float X; float Y; }
├── PointInt.cs # struct PointInt { int X; int Y; }
├── UnsafeAs.csproj # AllowUnsafeBlocks = true
└── UnsafeAs.sln
🧪 Development and testing
Build with dotnet build UnsafeAs/UnsafeAs.csproj, then use an interactive terminal to compare the output on your architecture and press a key to exit. This review checked the source and unsafe-block project setting; it did not execute memory reinterpretation examples.
🛠️ Troubleshooting and limitations
Notes
Unsafe.As is intended for performance-critical scenarios such as binary serialisation, network protocol parsing, and SIMD-style operations. Misuse can corrupt memory or produce incorrect results; always ensure source and target types have compatible sizes and alignment.
The output examples follow the source on a little-endian machine; they were not re-executed in this documentation review.
Memory layout, endianness, size and alignment affect these results. A reference reinterpretation is not a checked numeric conversion. The byte-array example assumes at least eight accessible bytes, and the shown values assume little-endian storage. The application waits for Console.ReadKey after printing output.
🔗 Related documentation
Related Examples
- Nilog — typed
ILoggerextensions that reduce call-site allocations in performance-sensitive .NET code - Back to all C# and .NET code examples
Documentation links
Read all C# and .NET Code Examples guides online · Project overview · All developer projects