C# has long offered raw pointer access via the `unsafe` keyword, but `Span<T>` has largely replaced that niche since 2018 by providing near-pointer performance with bounds checking and no pinning overhead. The post explains how `Span<T>` works internally via managed pointers (byrefs), demonstrates slicing and stackalloc without unsafe blocks, and presents BenchmarkDotNet results showing Span is 2-5x faster than naive array code while pointers edge it out only in narrow byte-reinterpretation scenarios. It then covers C# 16's upcoming overhaul of `unsafe`: moving from a broad context to a per-member contract with mandatory safety documentation, a new `safe` keyword for extern declarations, and pointer types (not dereferences) no longer being inherently unsafe. The guidance is to default to Span for performance-sensitive work and reserve raw pointers for specific interop or hot-path cases where Span genuinely falls short.

12m read timeFrom blog.ndepend.com
Post cover image
Table of contents
Why pointers existed in the first placeEnter Span<T>Benchmark: Reference vs. Span vs. Pointerunsafe Changes in C# 16So which one should you reach for?Further reading
7.2K Impressions